Skip to content

Commit

Permalink
add some formatted append functions to the string class
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 492571663
  • Loading branch information
ericsalo authored and copybara-github committed Dec 2, 2022
1 parent 65a329a commit a3e49f9
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions upb/io/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@
#ifndef UPB_IO_STRING_H_
#define UPB_IO_STRING_H_

#include <stdarg.h>
#include <stdlib.h>
#include <string.h>

#include "upb/mem/arena.h"
#include "upb/port/vsnprintf_compat.h"

// Must be last.
#include "upb/port/def.inc"
Expand Down Expand Up @@ -112,6 +115,35 @@ UPB_INLINE bool upb_String_Append(upb_String* s, const char* data,
return true;
}

UPB_PRINTF(2, 0)
UPB_INLINE bool upb_String_AppendFmtV(upb_String* s, const char* fmt,
va_list args) {
size_t capacity = 1000;
char* buf = (char*)malloc(capacity);
bool out = false;
for (;;) {
const int n = _upb_vsnprintf(buf, capacity, fmt, args);
if (n < 0) break;
if (n < capacity) {
out = upb_String_Append(s, buf, n);
break;
}
capacity *= 2;
buf = (char*)realloc(buf, capacity);
}
free(buf);
return out;
}

UPB_PRINTF(2, 3)
UPB_INLINE bool upb_String_AppendFmt(upb_String* s, const char* fmt, ...) {
va_list args;
va_start(args, fmt);
const bool ok = upb_String_AppendFmtV(s, fmt, args);
va_end(args);
return ok;
}

UPB_INLINE bool upb_String_Assign(upb_String* s, const char* data,
size_t size) {
upb_String_Clear(s);
Expand Down

0 comments on commit a3e49f9

Please sign in to comment.