CleanupStr This function strips all leading and trailing spaces, keeping internal spaces. This goes for tabs too.
| 46 | // CleanupStr |
| 47 | // This function strips all leading and trailing spaces, keeping internal spaces. This goes for tabs too. |
| 48 | std::size_t CleanupStr(char *dest, const char *src, std::size_t destlen) { |
| 49 | if (destlen == 0) |
| 50 | return 0; |
| 51 | |
| 52 | const char *end; |
| 53 | std::size_t out_size; |
| 54 | |
| 55 | // Trim leading space |
| 56 | while (std::isspace((uint8_t)*src)) |
| 57 | src++; |
| 58 | |
| 59 | // All spaces? |
| 60 | if (*src == '\0') { |
| 61 | *dest = '\0'; |
| 62 | return 1; |
| 63 | } |
| 64 | |
| 65 | // Trim trailing space |
| 66 | end = src + std::strlen(src) - 1; |
| 67 | while (end > src && std::isspace((uint8_t)*end)) |
| 68 | end--; |
| 69 | end++; |
| 70 | |
| 71 | // Set output size to minimum of trimmed string length and buffer size minus 1 |
| 72 | out_size = (end - src) < destlen - 1 ? (end - src) : destlen - 1; |
| 73 | |
| 74 | // Copy trimmed string and add null terminator |
| 75 | std::memcpy(dest, src, out_size); |
| 76 | dest[out_size] = '\0'; |
| 77 | |
| 78 | return out_size; |
| 79 | } |
no outgoing calls