Skip to content

Commit 6b993e2

Browse files
mechkoclaude
andcommitted
stleary#1071: extract inRange helper to satisfy Sonar S3776
isXmlNameStart's alternating &&/|| chain scored cognitive complexity 28. Extracting inRange(cp, lo, hi) collapses it to a flat || sequence and keeps the range list 1:1 with the XML 1.0 NameStartChar production. isXmlNameChar updated the same way. No behaviour change. Co-Authored-By: Claude <noreply@anthropic.com>
1 parent e2cfb5a commit 6b993e2

1 file changed

Lines changed: 25 additions & 19 deletions

File tree

‎src/main/java/org/json/XML.java‎

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -262,28 +262,33 @@ static void mustBeXmlName(String string) throws JSONException {
262262
}
263263
}
264264

265+
private static boolean inRange(int cp, int lo, int hi) {
266+
return cp >= lo && cp <= hi;
267+
}
268+
265269
/**
266270
* XML 1.0 (5th ed.) {@code NameStartChar} production.
267271
*
268272
* @param cp a Unicode code point
269273
* @return true if {@code cp} may start an XML Name
270274
*/
271275
private static boolean isXmlNameStart(int cp) {
272-
return cp == ':' || cp == '_'
273-
|| (cp >= 'A' && cp <= 'Z')
274-
|| (cp >= 'a' && cp <= 'z')
275-
|| (cp >= 0xC0 && cp <= 0xD6)
276-
|| (cp >= 0xD8 && cp <= 0xF6)
277-
|| (cp >= 0xF8 && cp <= 0x2FF)
278-
|| (cp >= 0x370 && cp <= 0x37D)
279-
|| (cp >= 0x37F && cp <= 0x1FFF)
280-
|| (cp >= 0x200C && cp <= 0x200D)
281-
|| (cp >= 0x2070 && cp <= 0x218F)
282-
|| (cp >= 0x2C00 && cp <= 0x2FEF)
283-
|| (cp >= 0x3001 && cp <= 0xD7FF)
284-
|| (cp >= 0xF900 && cp <= 0xFDCF)
285-
|| (cp >= 0xFDF0 && cp <= 0xFFFD)
286-
|| (cp >= 0x10000 && cp <= 0xEFFFF);
276+
return cp == ':'
277+
|| cp == '_'
278+
|| inRange(cp, 'A', 'Z')
279+
|| inRange(cp, 'a', 'z')
280+
|| inRange(cp, 0xC0, 0xD6)
281+
|| inRange(cp, 0xD8, 0xF6)
282+
|| inRange(cp, 0xF8, 0x2FF)
283+
|| inRange(cp, 0x370, 0x37D)
284+
|| inRange(cp, 0x37F, 0x1FFF)
285+
|| inRange(cp, 0x200C, 0x200D)
286+
|| inRange(cp, 0x2070, 0x218F)
287+
|| inRange(cp, 0x2C00, 0x2FEF)
288+
|| inRange(cp, 0x3001, 0xD7FF)
289+
|| inRange(cp, 0xF900, 0xFDCF)
290+
|| inRange(cp, 0xFDF0, 0xFFFD)
291+
|| inRange(cp, 0x10000, 0xEFFFF);
287292
}
288293

289294
/**
@@ -294,11 +299,12 @@ private static boolean isXmlNameStart(int cp) {
294299
*/
295300
private static boolean isXmlNameChar(int cp) {
296301
return isXmlNameStart(cp)
297-
|| cp == '-' || cp == '.'
298-
|| (cp >= '0' && cp <= '9')
302+
|| cp == '-'
303+
|| cp == '.'
299304
|| cp == 0xB7
300-
|| (cp >= 0x0300 && cp <= 0x036F)
301-
|| (cp >= 0x203F && cp <= 0x2040);
305+
|| inRange(cp, '0', '9')
306+
|| inRange(cp, 0x0300, 0x036F)
307+
|| inRange(cp, 0x203F, 0x2040);
302308
}
303309

304310
/**

0 commit comments

Comments
 (0)