| 41 | namespace ceres::internal { |
| 42 | |
| 43 | void StringAppendV(std::string* dst, const char* format, va_list ap) { |
| 44 | // First try with a small fixed size buffer |
| 45 | char space[1024]; |
| 46 | |
| 47 | // It's possible for methods that use a va_list to invalidate |
| 48 | // the data in it upon use. The fix is to make a copy |
| 49 | // of the structure before using it and use that copy instead. |
| 50 | va_list backup_ap; |
| 51 | va_copy(backup_ap, ap); |
| 52 | int result = vsnprintf(space, sizeof(space), format, backup_ap); |
| 53 | va_end(backup_ap); |
| 54 | |
| 55 | if (result < sizeof(space)) { |
| 56 | if (result >= 0) { |
| 57 | // Normal case -- everything fit. |
| 58 | dst->append(space, result); |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | #if defined(_MSC_VER) |
| 63 | // Error or MSVC running out of space. MSVC 8.0 and higher |
| 64 | // can be asked about space needed with the special idiom below: |
| 65 | va_copy(backup_ap, ap); |
| 66 | result = vsnprintf(nullptr, 0, format, backup_ap); |
| 67 | va_end(backup_ap); |
| 68 | #endif |
| 69 | |
| 70 | if (result < 0) { |
| 71 | // Just an error. |
| 72 | return; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // Increase the buffer size to the size requested by vsnprintf, |
| 77 | // plus one for the closing \0. |
| 78 | int length = result + 1; |
| 79 | char* buf = new char[length]; |
| 80 | |
| 81 | // Restore the va_list before we use it again |
| 82 | va_copy(backup_ap, ap); |
| 83 | result = vsnprintf(buf, length, format, backup_ap); |
| 84 | va_end(backup_ap); |
| 85 | |
| 86 | if (result >= 0 && result < length) { |
| 87 | // It fit |
| 88 | dst->append(buf, result); |
| 89 | } |
| 90 | delete[] buf; |
| 91 | } |
| 92 | |
| 93 | std::string StringPrintf(const char* format, ...) { |
| 94 | va_list ap; |
no outgoing calls
no test coverage detected