Skip to content

Commit

Permalink
Merge pull request #69 from fredriv/simplify-trim
Browse files Browse the repository at this point in the history
Simplify trim() function
  • Loading branch information
larsga authored Apr 1, 2019
2 parents bdd7ca5 + c22eca7 commit 8e3d334
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 8e3d334

Please sign in to comment.