@CompilerDirectives.SlowPath @Specialization(order = 1) public Object getIndex(RubyString string, int index, UndefinedPlaceholder undefined) { final String javaString = string.toString(); final int normalisedIndex = ArrayUtilities.normaliseIndex(javaString.length(), index); return getContext().makeString(javaString.charAt(normalisedIndex)); }
@CompilerDirectives.SlowPath @Specialization(order = 2) public Object getIndex(RubyString string, FixnumRange range, UndefinedPlaceholder undefined) { final String javaString = string.toString(); final int stringLength = javaString.length(); if (range.doesExcludeEnd()) { final int begin = ArrayUtilities.normaliseIndex(stringLength, range.getBegin()); final int exclusiveEnd = ArrayUtilities.normaliseExclusiveIndex(stringLength, range.getExclusiveEnd()); return getContext().makeString(javaString.substring(begin, exclusiveEnd)); } else { final int begin = ArrayUtilities.normaliseIndex(stringLength, range.getBegin()); final int inclusiveEnd = ArrayUtilities.normaliseIndex(stringLength, range.getInclusiveEnd()); return getContext().makeString(javaString.substring(begin, inclusiveEnd + 1)); } }