MCPcopy Create free account
hub / github.com/instantdb/instant / appendLogfmtString

Method appendLogfmtString

server/src/java/instant/Logfmt.java:25–84  ·  view source on GitHub ↗

Append s to sb as a logfmt value: Bare if the string is safe — no space, =, or escape char. Otherwise wrapped in "..." with ", \, \n, \r, \t backslash-escaped. null and empty stri

(StringBuilder sb, String s)

Source from the content-addressed store, hash-verified

23 * escapes is copied in one batched range-append.
24 */
25 public static void appendLogfmtString(StringBuilder sb, String s) {
26 if (s == null || s.isEmpty()) {
27 sb.append("\"\"");
28 return;
29 }
30 int n = s.length();
31
32 // First pass: find the earliest char that forces quoting. Bare logfmt
33 // values can't contain space, =, or any of the five escape chars.
34 int trigger = -1;
35 for (int k = 0; k < n; k++) {
36 char c = s.charAt(k);
37 if (c == ' ' || c == '=' || c == '"' || c == '\\'
38 || c == '\n' || c == '\r' || c == '\t') {
39 trigger = k;
40 break;
41 }
42 }
43 if (trigger < 0) {
44 sb.append(s);
45 return;
46 }
47
48 sb.append('"');
49 sb.append(s, 0, trigger);
50
51 // Inside the quoted region only the five real escape chars matter; space
52 // and = are now plain content. Skip ahead from one escape to the next,
53 // batch-copying the safe span between them.
54 int i = trigger;
55 while (i < n) {
56 int escapeAt = -1;
57 for (int k = i; k < n; k++) {
58 char c = s.charAt(k);
59 if (c == '"' || c == '\\' || c == '\n' || c == '\r' || c == '\t') {
60 escapeAt = k;
61 break;
62 }
63 }
64 if (escapeAt < 0) {
65 sb.append(s, i, n);
66 break;
67 }
68 sb.append(s, i, escapeAt);
69 switch (s.charAt(escapeAt)) {
70 case '"': sb.append("\\\""); break;
71 case '\\': sb.append("\\\\"); break;
72 case '\n': sb.append("\\n"); break;
73 case '\r': sb.append("\\r"); break;
74 case '\t': sb.append("\\t"); break;
75 default:
76 throw new AssertionError(
77 "Logfmt escape loop landed on unhandled char " +
78 "0x" + Integer.toHexString(s.charAt(escapeAt)) +
79 " at index " + escapeAt);
80 }
81 i = escapeAt + 1;
82 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected