Skip to content

Commit ab4052d

Browse files
author
alwinsanil
committed
Complex conditional: JSONArray.nextTo code smell fix
1 parent d899865 commit ab4052d

1 file changed

Lines changed: 47 additions & 19 deletions

File tree

src/main/java/org/json/JSONTokener.java

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -400,26 +400,15 @@ public String nextString(char quote) throws JSONException {
400400
/**
401401
* Get the text up but not including the specified character or the
402402
* end of line, whichever comes first.
403-
* @param delimiter A delimiter character.
404-
* @return A string.
403+
* @param delimiter A delimiter character.
404+
* @return A string.
405405
* @throws JSONException Thrown if there is an error while searching
406406
* for the delimiter
407407
*/
408408
public String nextTo(char delimiter) throws JSONException {
409-
StringBuilder sb = new StringBuilder();
410-
for (;;) {
411-
char c = this.next();
412-
if (c == delimiter || c == 0 || c == '\n' || c == '\r') {
413-
if (c != 0) {
414-
this.back();
415-
}
416-
return sb.toString().trim();
417-
}
418-
sb.append(c);
419-
}
409+
return nextToInternal(c -> c == delimiter);
420410
}
421411

422-
423412
/**
424413
* Get the text up but not including one of the specified delimiter
425414
* characters or the end of line, whichever comes first.
@@ -429,13 +418,22 @@ public String nextTo(char delimiter) throws JSONException {
429418
* for the delimiter
430419
*/
431420
public String nextTo(String delimiters) throws JSONException {
432-
char c;
421+
return nextToInternal(c -> delimiters.indexOf(c) >= 0);
422+
}
423+
424+
/**
425+
* Internal implementation for nextTo operations.
426+
* @param delimiterCheck Function to check if character is a delimiter
427+
* @return Collected string up to delimiter
428+
* @throws JSONException Thrown if there is an error while reading
429+
*/
430+
private String nextToInternal(java.util.function.Predicate<Character> delimiterCheck)
431+
throws JSONException {
433432
StringBuilder sb = new StringBuilder();
434433
for (;;) {
435-
c = this.next();
436-
if (delimiters.indexOf(c) >= 0 || c == 0 ||
437-
c == '\n' || c == '\r') {
438-
if (c != 0) {
434+
char c = this.next();
435+
if (shouldStopReading(c, delimiterCheck)) {
436+
if (!isEndOfInput(c)) {
439437
this.back();
440438
}
441439
return sb.toString().trim();
@@ -444,6 +442,36 @@ public String nextTo(String delimiters) throws JSONException {
444442
}
445443
}
446444

445+
/**
446+
* Determines whether reading should stop based on the current character.
447+
* @param c The current character to check
448+
* @param delimiterCheck Predicate to test for delimiter matches
449+
* @return true if reading should stop, false otherwise
450+
*/
451+
private boolean shouldStopReading(char c, java.util.function.Predicate<Character> delimiterCheck) {
452+
boolean isEndOfInput = isEndOfInput(c);
453+
boolean isNewLine = isNewLineCharacter(c);
454+
return delimiterCheck.test(c) || isEndOfInput || isNewLine;
455+
}
456+
457+
/**
458+
* Checks if a character represents the end of input.
459+
* @param c The character to check
460+
* @return true if the character is the end-of-input marker (0), false otherwise
461+
*/
462+
private boolean isEndOfInput(char c) {
463+
return c == 0;
464+
}
465+
466+
/**
467+
* Checks if a character represents a newline.
468+
* @param c The character to check
469+
* @return true if the character is a newline (\n or \r), false otherwise
470+
*/
471+
private boolean isNewLineCharacter(char c) {
472+
return c == '\n' || c == '\r';
473+
}
474+
447475

448476
/**
449477
* Get the next value. The value can be a Boolean, Double, Integer,

0 commit comments

Comments
 (0)