MCPcopy Create free account
hub / github.com/OpenStarbound/OpenStarbound / detectWhitespace

Function detectWhitespace

source/core/StarFormattedJson.cpp:165–213  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

163}
164
165WhitespaceStyle detectWhitespace(FormattedJson::ElementList const& elements,
166 FormattedJson::ElementLocation insertLoc, bool array) {
167 WhitespaceStyle style;
168
169 // Find a nearby value as a reference location to learn whitespace from.
170 FormattedJson::ElementLocation valueLoc = lastIndexOf<ValueElement>(elements, insertLoc);
171 if (valueLoc == NPos)
172 valueLoc = indexOf<ValueElement>(elements, insertLoc);
173
174 if (valueLoc == NPos) {
175 // This object/array is empty. Pre-key/value whitespace will be the total of
176 // the whitespace already present, plus some guessed indentation if it
177 // contained a newline.
178 String beforeValue = concatWhitespace(elements, 0, elements.size());
179 if (beforeValue.find('\n') != NPos)
180 beforeValue += " ";
181 if (array)
182 return WhitespaceStyle{"", "", beforeValue, ""};
183 return WhitespaceStyle{beforeValue, "", "", ""};
184 }
185
186 FormattedJson::ElementLocation commaLoc = indexOf<CommaElement>(elements, valueLoc);
187 if (commaLoc != NPos) {
188 style.beforeComma = concatWhitespace(elements, valueLoc + 1, commaLoc);
189 }
190
191 FormattedJson::ElementLocation colonLoc = lastIndexOf<ColonElement>(elements, valueLoc);
192 starAssert((colonLoc == NPos) == array);
193 if (colonLoc != NPos) {
194 style.beforeValue = concatWhitespace(elements, colonLoc + 1, valueLoc);
195
196 FormattedJson::ElementLocation keyLoc = lastIndexOf<ObjectKeyElement>(elements, colonLoc);
197 starAssert(keyLoc != NPos);
198 style.beforeColon = concatWhitespace(elements, keyLoc + 1, colonLoc);
199
200 FormattedJson::ElementLocation prevValueLoc = lastIndexOf<ValueElement>(elements, keyLoc);
201 if (prevValueLoc == NPos)
202 prevValueLoc = 0;
203 style.beforeKey = concatWhitespace(elements, prevValueLoc, keyLoc);
204
205 } else {
206 FormattedJson::ElementLocation prevValueLoc = lastIndexOf<ValueElement>(elements, valueLoc);
207 if (prevValueLoc == NPos)
208 prevValueLoc = 0;
209 style.beforeValue = concatWhitespace(elements, prevValueLoc, valueLoc);
210 }
211
212 return style;
213}
214
215void insertWhitespace(FormattedJson::ElementList& destination, FormattedJson::ElementLocation& at, String const& whitespace) {
216 if (whitespace == "")

Callers 1

Calls 3

concatWhitespaceFunction · 0.85
sizeMethod · 0.45
findMethod · 0.45

Tested by

no test coverage detected