Package com.oracle.truffle.api.strings
Class TruffleString.ByteIndexOfCodePointSetNode
java.lang.Object
com.oracle.truffle.api.nodes.Node
com.oracle.truffle.api.strings.TruffleString.ByteIndexOfCodePointSetNode
- All Implemented Interfaces:
NodeInterface
,Cloneable
- Enclosing class:
TruffleString
Node to find the byte index of the first occurrence of a codepoint present in a given
codepoint set. See
execute(AbstractTruffleString, int, int, TruffleString.CodePointSet)
for details.- Since:
- 23.0
-
Nested Class Summary
Nested classes/interfaces inherited from class com.oracle.truffle.api.nodes.Node
Node.Child, Node.Children
-
Method Summary
Modifier and TypeMethodDescriptioncreate()
Create a newTruffleString.ByteIndexOfCodePointSetNode
.final int
execute
(AbstractTruffleString a, int fromByteIndex, int toByteIndex, TruffleString.CodePointSet codePointSet) Returns the byte index of the first codepoint present in the givenTruffleString.CodePointSet
, bounded byfromByteIndex
(inclusive) andtoByteIndex
(exclusive).abstract int
execute
(AbstractTruffleString a, int fromByteIndex, int toByteIndex, TruffleString.CodePointSet codePointSet, boolean usePreciseCodeRange) Returns the byte index of the first codepoint present in the givenTruffleString.CodePointSet
, bounded byfromByteIndex
(inclusive) andtoByteIndex
(exclusive).Get the uncached version ofTruffleString.ByteIndexOfCodePointSetNode
.Methods inherited from class com.oracle.truffle.api.nodes.Node
accept, adoptChildren, atomic, atomic, copy, deepCopy, getChildren, getCost, getDebugProperties, getDescription, getEncapsulatingSourceSection, getLock, getParent, getRootNode, getSourceSection, insert, insert, isAdoptable, isSafelyReplaceableBy, notifyInserted, onReplace, replace, replace, reportPolymorphicSpecialize, reportReplace, toString
-
Method Details
-
execute
public final int execute(AbstractTruffleString a, int fromByteIndex, int toByteIndex, TruffleString.CodePointSet codePointSet) Returns the byte index of the first codepoint present in the givenTruffleString.CodePointSet
, bounded byfromByteIndex
(inclusive) andtoByteIndex
(exclusive).TruffleString.ByteIndexOfCodePointSetNode
will specialize on the givenTruffleString.CodePointSet
's content, which is therefore required to bepartial evaluation constant
.Usage example: A node that scans a string for a known set of code points and escapes them with '\'.
abstract static class StringEscapeNode extends Node { public static final TruffleString.Encoding ENCODING = TruffleString.Encoding.UTF_32; public static final TruffleString.ByteIndexOfCodePointSetNode.CodePointSet ESCAPE_CHARS = TruffleString.ByteIndexOfCodePointSetNode.CodePointSet.fromRanges(new int[]{ '\n', '\n', '\r', '\r', // .... }, ENCODING); abstract TruffleString execute(TruffleString input); @Specialization static TruffleString run(TruffleString input, @Cached TruffleString.ByteIndexOfCodePointSetNode byteIndexOfCodePointSetNode, @Cached TruffleStringBuilder.AppendSubstringByteIndexNode appendSubstringByteIndexNode, @Cached TruffleString.CodePointAtByteIndexNode codePointAtByteIndexNode, @Cached TruffleStringBuilder.AppendCodePointNode appendCodePointNode, @Cached TruffleString.ByteLengthOfCodePointNode byteLengthOfCodePointNode, @Cached TruffleStringBuilder.ToStringNode toStringNode) { int byteLength = input.byteLength(ENCODING); TruffleStringBuilder sb = TruffleStringBuilder.create(ENCODING, byteLength); int lastPos = 0; int pos = 0; while (pos >= 0) { pos = byteIndexOfCodePointSetNode.execute(input, lastPos, byteLength, ESCAPE_CHARS); int substringLength = (pos < 0 ? byteLength : pos) - lastPos; appendSubstringByteIndexNode.execute(sb, input, lastPos, substringLength); if (pos >= 0) { int codePoint = codePointAtByteIndexNode.execute(input, pos, ENCODING); appendCodePointNode.execute(sb, '\\'); appendCodePointNode.execute(sb, codePoint); int codePointLength = byteLengthOfCodePointNode.execute(input, pos, ENCODING); lastPos = pos + codePointLength; } } return toStringNode.execute(sb); } }
- Parameters:
codePointSet
- The set of codepoints to look for. This parameter is expected to bepartial evaluation constant
.- Since:
- 23.0
-
execute
public abstract int execute(AbstractTruffleString a, int fromByteIndex, int toByteIndex, TruffleString.CodePointSet codePointSet, boolean usePreciseCodeRange) Returns the byte index of the first codepoint present in the givenTruffleString.CodePointSet
, bounded byfromByteIndex
(inclusive) andtoByteIndex
(exclusive).- Parameters:
usePreciseCodeRange
- If this parameter is set totrue
, the node may evaluate the input string's precise code range for better search performance. For more details, seeTruffleString.GetCodeRangeNode
andTruffleString.GetCodeRangeImpreciseNode
. This parameter is expected to bepartial evaluation constant
.- Since:
- 24.1
-
create
Create a newTruffleString.ByteIndexOfCodePointSetNode
.- Since:
- 23.0
-
getUncached
Get the uncached version ofTruffleString.ByteIndexOfCodePointSetNode
.- Since:
- 23.0
-