Skip to content

Commit

Permalink
Simplify trim() function
Browse files Browse the repository at this point in the history
  • Loading branch information
Fredrik Vraalsen committed Apr 1, 2019
1 parent bdd7ca5 commit c22eca7
Showing 1 changed file with 1 addition and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -828,40 +828,7 @@ public JsonNode call(JsonNode input, JsonNode[] arguments) {
if (string == null)
return NullNode.instance;

int prefix = 0; // first non-space character

// first: find leading spaces
for (;
prefix < string.length() && isSpace(string, prefix);
prefix++)
;

// if the whole thing is spaces we're done
if (prefix == string.length())
return new TextNode("");

int suffix = string.length() - 1; // last non-space character

// second: find trailing spaces
for (;
suffix >= 0 && isSpace(string, suffix);
suffix--)
;

// INV suffix >= prefix (they're equal if non-space part is 1 character)

// if there are no leading or trailing spaces: keep input
if (prefix == 0 && suffix == string.length() - 1 &&
arguments[0].isTextual())
return arguments[0];

// copy out middle section and we're done
return new TextNode(string.substring(prefix, suffix + 1));
}

private static boolean isSpace(String string, int ix) {
char ch = string.charAt(ix);
return ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n';
return new TextNode(string.trim());
}
}

Expand Down

0 comments on commit c22eca7

Please sign in to comment.