MCPcopy Create free account
hub / github.com/albertodemichelis/squirrel / sqstd_format

Function sqstd_format

sqstdlib/sqstdstring.cpp:70–157  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

68}
69
70SQRESULT sqstd_format(HSQUIRRELVM v,SQInteger nformatstringidx,SQInteger *outlen,SQChar **output)
71{
72 const SQChar *format;
73 SQChar *dest;
74 SQChar fmt[MAX_FORMAT_LEN];
75 const SQRESULT res = sq_getstring(v,nformatstringidx,&format);
76 if (SQ_FAILED(res)) {
77 return res; // propagate the error
78 }
79 SQInteger format_size = sq_getsize(v,nformatstringidx);
80 SQInteger allocated = (format_size+2)*sizeof(SQChar);
81 dest = sq_getscratchpad(v,allocated);
82 SQInteger n = 0,i = 0, nparam = nformatstringidx+1, w = 0;
83 //while(format[n] != '\0')
84 while(n < format_size)
85 {
86 if(format[n] != '%') {
87 assert(i < allocated);
88 dest[i++] = format[n];
89 n++;
90 }
91 else if(format[n+1] == '%') { //handles %%
92 dest[i++] = '%';
93 n += 2;
94 }
95 else {
96 n++;
97 if( nparam > sq_gettop(v) )
98 return sq_throwerror(v,_SC("not enough parameters for the given format string"));
99 n = validate_format(v,fmt,format,n,w);
100 if(n < 0) return -1;
101 SQInteger addlen = 0;
102 SQInteger valtype = 0;
103 const SQChar *ts = NULL;
104 SQInteger ti = 0;
105 SQFloat tf = 0;
106 switch(format[n]) {
107 case 's':
108 if(SQ_FAILED(sq_getstring(v,nparam,&ts)))
109 return sq_throwerror(v,_SC("string expected for the specified format"));
110 addlen = (sq_getsize(v,nparam)*sizeof(SQChar))+((w+1)*sizeof(SQChar));
111 valtype = 's';
112 break;
113 case 'i': case 'd': case 'o': case 'u': case 'x': case 'X':
114#ifdef _SQ64
115 {
116 size_t flen = scstrlen(fmt);
117 SQInteger fpos = flen - 1;
118 SQChar f = fmt[fpos];
119 const SQChar *prec = (const SQChar *)_PRINT_INT_PREC;
120 while(*prec != _SC('\0')) {
121 fmt[fpos++] = *prec++;
122 }
123 fmt[fpos++] = f;
124 fmt[fpos++] = _SC('\0');
125 }
126#endif
127 case 'c':

Callers 2

_string_printfFunction · 0.85
_string_formatFunction · 0.85

Calls 8

sq_getstringFunction · 0.85
sq_getsizeFunction · 0.85
sq_getscratchpadFunction · 0.85
sq_gettopFunction · 0.85
sq_throwerrorFunction · 0.85
validate_formatFunction · 0.85
sq_getintegerFunction · 0.85
sq_getfloatFunction · 0.85

Tested by

no test coverage detected