void native_formatter_invoke(uint8_t mref) { char * fmt = stack_peek_addr(0); char res[50]; formatDescr fmtdscr; make_format_descr(&fmtdscr, fmt); int len = 0; if(mref == NATIVE_METHOD_formatI) { nvm_int_t val = stack_peek_int(1); len = format_int(res, &fmtdscr, val); } else if(mref == NATIVE_METHOD_formatZ) { nvm_int_t val = stack_peek_int(1); len = format_bool(res, &fmtdscr, val); } else if(mref == NATIVE_METHOD_formatF) { nvm_float_t val = stack_peek_float(1); len = format_float(res, &fmtdscr, val); } else error(ERROR_NATIVE_UNKNOWN_METHOD); uint8_t add = 0; if (fmtdscr.width>len) add = (fmtdscr.width-len); // allocate heap and realign strings (address may be changed by gc...) heap_id_t id = heap_alloc(FALSE, len + add + fmtdscr.pre_len + fmtdscr.post_len + 1); int memoffset = (char*)stack_peek_addr(0)-(char*)fmt; fmt+=memoffset; fmtdscr.post+=memoffset; char * dst = heap_get_addr(id); // build result string native_strncpy(dst, fmt, fmtdscr.pre_len); dst+=fmtdscr.pre_len; if (!(fmtdscr.flags&0x01)){ while(add--) *dst++=' '; } native_strncpy(dst, res, len); dst+=len; if (fmtdscr.flags&0x01){ while(add--) *dst++=' '; } native_strncpy(dst, fmtdscr.post, fmtdscr.post_len); dst+=fmtdscr.post_len; *dst=0; stack_pop(); stack_pop(); stack_push(NVM_TYPE_HEAP | id); }
// append a string to another one void native_strncat(char *dst, const char *src, int n) { while(n--&&(*dst)) dst++; // skip string native_strncpy(dst, src, n); // attach it }