| 17155 | |
| 17156 | |
| 17157 | ResultType TokenSetResult(ResultToken &aResultToken, LPCTSTR aValue, size_t aLength) |
| 17158 | // Utility function for handling memory allocation and return to callers of built-in functions; based on BIF_SubStr. |
| 17159 | // Returns FAIL if malloc failed, in which case our caller is responsible for returning a sensible default value. |
| 17160 | { |
| 17161 | if (aLength == -1) |
| 17162 | aLength = _tcslen(aValue); // Caller must not pass NULL for aResult in this case. |
| 17163 | if (aLength <= MAX_NUMBER_LENGTH) // v1.0.46.01: Avoid malloc() for small strings. However, this improves speed by only 10% in a test where random 25-byte strings were extracted from a 700 KB string (probably because VC++'s malloc()/free() are very fast for small allocations). |
| 17164 | aResultToken.marker = aResultToken.buf; // Store the address of the result for the caller. |
| 17165 | else |
| 17166 | { |
| 17167 | // Caller has provided a mem_to_free (initially NULL) as a means of passing back memory we allocate here. |
| 17168 | // So if we change "result" to be non-NULL, the caller will take over responsibility for freeing that memory. |
| 17169 | if ( !(aResultToken.mem_to_free = tmalloc(aLength + 1)) ) // Out of memory. |
| 17170 | return aResultToken.MemoryError(); |
| 17171 | aResultToken.marker = aResultToken.mem_to_free; // Store the address of the result for the caller. |
| 17172 | } |
| 17173 | if (aValue) // Caller may pass NULL to retrieve a buffer of sufficient size. |
| 17174 | tmemcpy(aResultToken.marker, aValue, aLength); |
| 17175 | aResultToken.marker[aLength] = '\0'; // Must be done separately from the memcpy() because the memcpy() might just be taking a substring (i.e. long before result's terminator). |
| 17176 | aResultToken.marker_length = aLength; |
| 17177 | return OK; |
| 17178 | } |
| 17179 | |
| 17180 | |
| 17181 |
no test coverage detected