From b4ccaaef7af488ee9ccc15c28b9dcbfd4d1763e0 Mon Sep 17 00:00:00 2001 From: mcmonkey4eva Date: Fri, 15 Nov 2013 22:30:37 -0800 Subject: [PATCH] Minimize element.substring errors + fix math This will prevent stacktraces / failure to run if substrings are used with really *really* wrong input indexes. (There are situations where it can happen out of the scripters control, can't blame them) It also corrects the counting of the end index (the character at endIndex is not included in Java)... it should be in dScript. This way substring[1,1] returns one letter rather than nothing at all. This is generally what people logically expect to occur, though there may be trouble with 'endIndex - startIndex' not being equal to the returned length... anyone who gets that far in a calculation can figure out to add 1. --- src/main/java/net/aufdemrand/denizen/objects/Element.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/aufdemrand/denizen/objects/Element.java b/src/main/java/net/aufdemrand/denizen/objects/Element.java index 79ac23bc7d..d6c48c6ef2 100644 --- a/src/main/java/net/aufdemrand/denizen/objects/Element.java +++ b/src/main/java/net/aufdemrand/denizen/objects/Element.java @@ -671,11 +671,13 @@ else if (element.toLowerCase().contains(contains.toLowerCase())) int beginning_index = Integer.valueOf(attribute.getContext(1).split(",")[0]) - 1; int ending_index; if (attribute.getContext(1).split(",").length > 1) - ending_index = Integer.valueOf(attribute.getContext(1).split(",")[1]) - 1; + ending_index = Integer.valueOf(attribute.getContext(1).split(",")[1]); else ending_index = element.length(); if (beginning_index < 0) beginning_index = 0; + if (beginning_index > element.length()) beginning_index = element.length(); if (ending_index > element.length()) ending_index = element.length(); + if (ending_index < beginning_index) ending_index = beginning_index; return new Element(element.substring(beginning_index, ending_index)) .getAttribute(attribute.fulfill(1)); }