MCPcopy Create free account
hub / github.com/blues/note / scanForJSONValue

Function scanForJSONValue

lib/wire.go:72–137  ·  view source on GitHub ↗

Method to scan for the longest json field value within the buffer, for JSON field/value substitution, optimized for large ints (unix date values) or string with common prefixes

(buf []byte, field string)

Source from the content-addressed store, hash-verified

70// Method to scan for the longest json field value within the buffer, for JSON field/value substitution,
71// optimized for large ints (unix date values) or string with common prefixes
72func scanForJSONValue(buf []byte, field string) (tokenBuf []byte) {
73 scanFor := []byte("\"" + field + "\":")
74 scanForLen := len(scanFor)
75 occurrences := 0
76
77 bufScanIndex := 0
78 for bufScanIndex < len(buf) {
79
80 // Look for the next occurrence of the field we're looking for
81 i := bytes.Index(buf[bufScanIndex:], scanFor)
82 if i == -1 {
83 break
84 }
85 i += bufScanIndex
86
87 // Look for the end of the data value
88 token := []byte("")
89 for j := i; j < len(buf); j++ {
90 ch := buf[j]
91 if ch == ' ' || ch == ',' || ch == '}' {
92 break
93 }
94 token = append(token, ch)
95 }
96
97 // If we've not yet grabbed our first token, grab the entire thing
98 if len(tokenBuf) == 0 {
99 // For us to start looking for commonalities in a token,
100 // it must be at least this many characters of savings.
101 if (len(token) - scanForLen) >= 5 {
102 tokenBuf = token
103 occurrences = 1
104 }
105 } else {
106
107 // Look for the longest token that we still have in common
108 shrank := false
109 for j := 0; j < len(tokenBuf); j++ {
110 if j >= len(token) || tokenBuf[j] != token[j] {
111
112 // To shrink the token length, the token must at least have SOME in common with the original.
113 if (j - scanForLen) >= 4 {
114 tokenBuf = tokenBuf[:j]
115 }
116 shrank = true
117 break
118 }
119 }
120 if !shrank {
121 occurrences++
122 }
123
124 }
125
126 // Update the pointer so that we look for the next one
127 bufScanIndex = i + scanForLen
128
129 }

Callers 1

jsonCompressFunction · 0.85

Calls 1

logDebugFunction · 0.85

Tested by

no test coverage detected