diff --git a/smithy-rules-engine/src/main/java/software/amazon/smithy/rulesengine/language/syntax/expressions/functions/Substring.java b/smithy-rules-engine/src/main/java/software/amazon/smithy/rulesengine/language/syntax/expressions/functions/Substring.java index 70d5d41194c..e7119791886 100644 --- a/smithy-rules-engine/src/main/java/software/amazon/smithy/rulesengine/language/syntax/expressions/functions/Substring.java +++ b/smithy-rules-engine/src/main/java/software/amazon/smithy/rulesengine/language/syntax/expressions/functions/Substring.java @@ -122,18 +122,22 @@ public static String getSubstring(String value, int startIndex, int stopIndex, b } int len = value.length(); - if (startIndex < 0 || stopIndex > len || startIndex >= stopIndex) { + if (startIndex >= stopIndex || len < stopIndex) { return null; } - int from = reverse ? len - stopIndex : startIndex; - int to = reverse ? len - startIndex : stopIndex; - for (int i = from; i < to; i++) { + for (int i = 0; i < len; i++) { if (value.charAt(i) > 127) { return null; } } - return value.substring(from, to); + if (reverse) { + int revStart = len - stopIndex; + int revStop = len - startIndex; + return value.substring(revStart, revStop); + } else { + return value.substring(startIndex, stopIndex); + } } } diff --git a/smithy-rules-engine/src/test/resources/software/amazon/smithy/rulesengine/language/errorfiles/valid/substring.smithy b/smithy-rules-engine/src/test/resources/software/amazon/smithy/rulesengine/language/errorfiles/valid/substring.smithy index f76b3d279bc..42d93b922d4 100644 --- a/smithy-rules-engine/src/test/resources/software/amazon/smithy/rulesengine/language/errorfiles/valid/substring.smithy +++ b/smithy-rules-engine/src/test/resources/software/amazon/smithy/rulesengine/language/errorfiles/valid/substring.smithy @@ -158,7 +158,7 @@ use smithy.rules#endpointTests "documentation": "unicode characters always return `None`", "params": { "TestCaseId": "1", - "Input": "\uD83D\uDC31abcdef" + "Input": "abcdef\uD83D\uDC31" }, "expect": { "error": "No tests matched" @@ -168,7 +168,7 @@ use smithy.rules#endpointTests "documentation": "non-ascii cause substring to always return `None`", "params": { "TestCaseId": "1", - "Input": "ab\u0080cdef" + "Input": "abcdef\u0080" }, "expect": { "error": "No tests matched"