Skip to content

Commit

Permalink
GROOVY-11252: StringGroovyMethods should have a getCodePoints method
Browse files Browse the repository at this point in the history
  • Loading branch information
paulk-asert committed Dec 18, 2023
1 parent 6f8314f commit 770f041
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/main/java/org/codehaus/groovy/runtime/StringGroovyMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -1470,6 +1470,19 @@ public static char[] getChars(final CharSequence self) {
return self.toString().toCharArray();
}

/**
* Converts the given CharSequence into an array of code point values including any surrogate pairs
* as per {@link String#codePoints()}.
*
* @param self a CharSequence
* @return an array of ints
*
* @since 5.0.0
*/
public static int[] getCodePoints(final CharSequence self) {
return self.codePoints().toArray();
}

/**
* Finds the number of Strings matched to the given Matcher.
*
Expand Down
9 changes: 9 additions & 0 deletions src/test/groovy/GroovyCharSequenceMethodsTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,15 @@ class GroovyCharSequenceMethodsTest extends GroovyTestCase {
assert chars[-1] == 'r'
}

void testGetCodePoints() {
def ints = cs2.codePoints
assert ints instanceof int[]
assert ints.size() == 6
assert ints[0] == 70
assert ints[3] == 98
assert ints[-1] == 114
}

private enum Coin { penny, nickel, dime, quarter }
void testAsType() {
def csDime = makeCharSequence('dime')
Expand Down

0 comments on commit 770f041

Please sign in to comment.