From c22eca7902ca057c5d59b1a6d0c7da749502b18f Mon Sep 17 00:00:00 2001 From: Fredrik Vraalsen Date: Mon, 1 Apr 2019 16:23:24 +0200 Subject: [PATCH] Simplify trim() function --- .../spt/data/jslt/impl/BuiltinFunctions.java | 35 +------------------ 1 file changed, 1 insertion(+), 34 deletions(-) diff --git a/src/main/java/com/schibsted/spt/data/jslt/impl/BuiltinFunctions.java b/src/main/java/com/schibsted/spt/data/jslt/impl/BuiltinFunctions.java index c1df15cb..ee943ce1 100644 --- a/src/main/java/com/schibsted/spt/data/jslt/impl/BuiltinFunctions.java +++ b/src/main/java/com/schibsted/spt/data/jslt/impl/BuiltinFunctions.java @@ -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()); } }