LCOV - code coverage report
Current view: top level - utils - StringUtils.h (source / functions) Coverage Total Hit
Test: coverage_filtered.info Lines: 87.5 % 24 21
Test Date: 2026-03-18 19:01:10 Functions: 100.0 % 4 4
Branches: 50.0 % 18 9

             Branch data     Line data    Source code
       1                 :             : #ifndef STRINGUTILS_H
       2                 :             : #define STRINGUTILS_H
       3                 :             : 
       4                 :             : #include <cstring>
       5                 :             : #include <cstdio>
       6                 :             : #include <cstdarg>
       7                 :             : 
       8                 :             : namespace StringUtils {
       9                 :             : 
      10                 :             : // Safe string copy method compatible with Windows and Linux.
      11                 :             : // It will always null terminate the destination buffer.
      12                 :             : // The dest_size is the total size of the destination buffer including the null terminator.
      13                 :         460 : inline void SafeCopy(char* dest, size_t dest_size, const char* src) {
      14   [ +  -  -  + ]:         460 :     if (!dest || dest_size == 0) return;
      15         [ -  + ]:         460 :     if (!src) {
      16                 :           0 :         dest[0] = '\0';
      17                 :           0 :         return;
      18                 :             :     }
      19                 :             : #ifdef _WIN32
      20                 :             :     strncpy_s(dest, dest_size, src, _TRUNCATE);
      21                 :             : #else
      22                 :         460 :     strncpy(dest, src, dest_size - 1);
      23                 :         460 :     dest[dest_size - 1] = '\0';
      24                 :             : #endif
      25                 :             : }
      26                 :             : 
      27                 :             : // Internal helper for variadic formatting
      28                 :       18565 : inline int vSafeFormat(char* dest, size_t dest_size, const char* format, va_list args) {
      29   [ +  -  -  + ]:       18565 :     if (!dest || dest_size == 0) return -1;
      30                 :             : #ifdef _WIN32
      31                 :             :     return vsnprintf_s(dest, dest_size, _TRUNCATE, format, args);
      32                 :             : #else
      33                 :       18565 :     int result = vsnprintf(dest, dest_size, format, args);
      34   [ +  -  -  + ]:       18565 :     if (result >= (int)dest_size || result < 0) {
      35                 :           0 :         dest[dest_size - 1] = '\0';
      36                 :             :     }
      37                 :       18565 :     return result;
      38                 :             : #endif
      39                 :             : }
      40                 :             : 
      41                 :             : // Safe formatted print method compatible with Windows and Linux.
      42                 :             : // It will always null terminate the destination buffer.
      43                 :        9914 : inline int SafeFormat(char* dest, size_t dest_size, const char* format, ...) {
      44                 :             :     va_list args;
      45                 :        9914 :     va_start(args, format);
      46                 :        9914 :     int result = vSafeFormat(dest, dest_size, format, args);
      47                 :        9914 :     va_end(args);
      48                 :        9914 :     return result;
      49                 :             : }
      50                 :             : 
      51                 :             : // Safe formatted scan method compatible with Windows and Linux.
      52                 :           8 : inline int SafeScan(const char* src, const char* format, ...) {
      53   [ +  -  -  + ]:           8 :     if (!src || !format) return -1;
      54                 :             :     va_list args;
      55                 :           8 :     va_start(args, format);
      56                 :           8 :     int result = vsscanf(src, format, args);
      57                 :           8 :     va_end(args);
      58                 :           8 :     return result;
      59                 :             : }
      60                 :             : 
      61                 :             : } // namespace StringUtils
      62                 :             : 
      63                 :             : #endif // STRINGUTILS_H
        

Generated by: LCOV version 2.0-1