Class StringUtils
java.lang.Object
org.daiitech.naftah.builtin.utils.StringUtils
Utility class for performing various operations on
String objects,
including arithmetic, bitwise, and vectorized character-wise operations.
Supports both scalar and vector APIs for enhanced performance on large strings.
The vector operations are backed by the ShortVector API.
This class also defines several functional constants used to map operations across strings.
This class is not instantiable.
- Author:
- Chakib Daii
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final BiFunction<Character,Character, Integer> Character addition (based on ASCII code points).static final BiFunction<ShortVector,ShortVector, ShortVector> Vectorized addition of characters.static final BiFunction<Character,Character, Integer> Bitwise AND operation for two characters.static final BiFunction<ShortVector,ShortVector, ShortVector> Bitwise AND for vectorized characters.static final Map<BiFunction<Character,Character, Integer>, BiFunction<ShortVector, ShortVector, ShortVector>> Mapping from scalar binary operations to vectorized equivalents.static final BiFunction<Character,Character, Integer> Character division.static final BiFunction<ShortVector,ShortVector, ShortVector> Vectorized division of characters.static final BiFunction<Character,Character, Integer> Character modulo.static final BiFunction<ShortVector,ShortVector, ShortVector> Vectorized modulo operation (simulatesfloorMod).static final BiFunction<Character,Character, Integer> Character multiplication.static final BiFunction<ShortVector,ShortVector, ShortVector> Vectorized multiplication of characters.Bitwise NOT operation for a single character.static final Function<ShortVector,ShortVector> Bitwise NOT for a vectorized character.static final BiFunction<Character,Character, Integer> Bitwise OR operation for two characters.static final BiFunction<ShortVector,ShortVector, ShortVector> Bitwise OR for vectorized characters.Post-decrement a character (based on ASCII code).Post-increment a character (based on ASCII code).Pre-decrement a character (based on ASCII code).static final Function<ShortVector,ShortVector> Vectorized pre-decrement.Pre-increment a character (based on ASCII code).static final Function<ShortVector,ShortVector> Vectorized pre-increment.private static final VectorSpecies<Short>Preferred vector species for character-based operations.static final BiFunction<Character,Character, Integer> Character subtraction.static final BiFunction<ShortVector,ShortVector, ShortVector> Vectorized subtraction of characters.static final Map<Function<Character,Number>, Function<ShortVector, ShortVector>> Mapping from scalar unary operations to vectorized equivalents.static final BooleanIndicates whether vectorized operations should be used (controlled via JVM property).static final intMinimum string length for vectorized execution.static final BiFunction<Character,Character, Integer> Bitwise XOR operation for two characters.static final BiFunction<ShortVector,ShortVector, ShortVector> Bitwise XOR for vectorized characters. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivatePrivate constructor to prevent instantiation. -
Method Summary
Modifier and TypeMethodDescriptionstatic StringConcatenates two strings.static StringPerforms bitwise AND between characters in two strings.static StringapplyOperation(String a, String b, BiFunction<Character, Character, Integer> operation) Applies a binary character-wise operation to two strings.static StringapplyOperation(String a, Function<Character, Number> operation) Applies a unary character-wise operation to a string.static StringapplyOperationScalar(String a, String b, BiFunction<Character, Character, Integer> operation) Applies a scalar binary character-wise operation to two strings.static StringapplyOperationScalar(String a, Function<Character, Number> operation) Applies a scalar unary character-wise operation to a string.static StringapplyOperationVectorized(String a, String b, BiFunction<Character, Character, Integer> operation, BiFunction<ShortVector, ShortVector, ShortVector> vectorOperation) Applies a vectorized binary operation to two strings usingShortVector.static StringapplyOperationVectorized(String input, Function<Character, Number> scalarOperation, Function<ShortVector, ShortVector> vectorOperation) Applies a vectorized unary operation usingShortVector.static StringcharWiseAdd(String a, String b) Performs character-wise addition between two strings.static StringcharWiseDivide(String a, String b) Performs character-wise division between two strings.static StringcharWiseModulo(String a, String b) Performs character-wise modulo between two strings.static StringcharWiseMultiply(String a, String b) Performs character-wise multiplication between two strings.static StringcharWiseSubtract(String a, String b) Performs character-wise subtraction between two strings.static intCompares two strings lexicographically.static String[]Splits a string into a specified number of parts.static String[]Splits a string using the given delimiter.static chardoApplyOperation(char aChar, char bChar, BiFunction<Character, Character, Integer> operation) Applies a scalar binary operation on two characters.static chardoApplyOperation(char aChar, Function<Character, Number> operation) Applies a unary operation on a single character.static booleanChecks if two strings are equal.static StringRepeats the given string a specified number of times.static NaftahBugErrorCreates aNaftahBugErrorindicating that the input string was null or empty.static NaftahBugErrorCreates aNaftahBugErrorindicating that the number of parts exceeds the string length.static NaftahBugErrorCreates aNaftahBugErrorindicating that the number of parts is zero or negative.static StringPerforms bitwise NOT operation on each character of the string.static StringPerforms bitwise OR between characters in two strings.static StringPost-decrements each character in the string.static StringPost-increments each character in the string.static StringPre-decrements each character in the string.static StringPre-increments each character in the string.static intConverts a string into an integer by summing its Unicode code points.static StringRemoves all occurrences ofbfroma.static StringPerforms bitwise XOR between characters in two strings.
-
Field Details
-
XOR
Bitwise XOR operation for two characters. -
AND
Bitwise AND operation for two characters. -
OR
Bitwise OR operation for two characters. -
NOT
Bitwise NOT operation for a single character. -
ADD
Character addition (based on ASCII code points). -
SUBTRACT
Character subtraction. -
MUL
Character multiplication. -
DIV
Character division. -
MOD
Character modulo. -
PRE_INCREMENT
Pre-increment a character (based on ASCII code). -
POST_INCREMENT
Post-increment a character (based on ASCII code). -
PRE_DECREMENT
Pre-decrement a character (based on ASCII code). -
POST_DECREMENT
Post-decrement a character (based on ASCII code). -
USE_VECTOR_API
Indicates whether vectorized operations should be used (controlled via JVM property). -
VECTOR_THRESHOLD
public static final int VECTOR_THRESHOLDMinimum string length for vectorized execution.- See Also:
-
XOR_VEC
Bitwise XOR for vectorized characters. -
AND_VEC
Bitwise AND for vectorized characters. -
OR_VEC
Bitwise OR for vectorized characters. -
NOT_VEC
Bitwise NOT for a vectorized character. -
ADD_VEC
Vectorized addition of characters. -
SUBTRACT_VEC
Vectorized subtraction of characters. -
MUL_VEC
Vectorized multiplication of characters. -
DIV_VEC
Vectorized division of characters. -
MOD_VEC
Vectorized modulo operation (simulatesfloorMod). -
PRE_INCREMENT_VEC
Vectorized pre-increment. -
PRE_DECREMENT_VEC
Vectorized pre-decrement. -
BINARY_OP_MAP
public static final Map<BiFunction<Character,Character, BINARY_OP_MAPInteger>, BiFunction<ShortVector, ShortVector, ShortVector>> Mapping from scalar binary operations to vectorized equivalents. -
UNARY_OP_MAP
Mapping from scalar unary operations to vectorized equivalents. -
SPECIES
Preferred vector species for character-based operations.
-
-
Constructor Details
-
StringUtils
private StringUtils()Private constructor to prevent instantiation. Always throws aNaftahBugErrorwhen called.
-
-
Method Details
-
equals
Checks if two strings are equal.- Parameters:
a- the first stringb- the second string- Returns:
trueif both strings are equal
-
compare
Compares two strings lexicographically.- Parameters:
a- the first stringb- the second string- Returns:
- a negative integer, zero, or a positive integer as
ais less than, equal to, or greater thanb
-
add
Concatenates two strings.- Parameters:
a- the first stringb- the second string- Returns:
- the concatenated string
-
subtract
Removes all occurrences ofbfroma.- Parameters:
a- the original stringb- the substring to remove- Returns:
- the resulting string
-
divide
Splits a string using the given delimiter.- Parameters:
a- the string to splitdelimiter- the delimiter- Returns:
- an array of substrings
-
divide
Splits a string into a specified number of parts.- Parameters:
s- the string to splitparts- the number of parts- Returns:
- an array containing the parts
- Throws:
NaftahBugError- if the string is empty, or ifpartsis invalid
-
multiply
Repeats the given string a specified number of times.- Parameters:
s- the input stringmultiplier- number of repetitions- Returns:
- the repeated string
-
applyOperation
public static String applyOperation(String a, String b, BiFunction<Character, Character, Integer> operation) Applies a binary character-wise operation to two strings.- If either
aorbisnull, throws aNaftahBugErrorindicating invalid input. - If one string is empty, returns the other string as-is. - Otherwise, applies the provided character-wise operation. If vectorization is enabled and applicable, uses a vectorized implementation for performance.- Parameters:
a- the first string (must not benull)b- the second string (must not benull)operation- the character-wise binary operation (e.g. addition, subtraction)- Returns:
- the resulting string after applying the operation
- Throws:
NaftahBugError- if eitheraorbisnull
-
applyOperationScalar
public static String applyOperationScalar(String a, String b, BiFunction<Character, Character, Integer> operation) Applies a scalar binary character-wise operation to two strings.- Parameters:
a- the first stringb- the second stringoperation- the binary operation- Returns:
- the result string after applying the operation character-wise
-
applyOperationVectorized
public static String applyOperationVectorized(String a, String b, BiFunction<Character, Character, Integer> operation, BiFunction<ShortVector, ShortVector, ShortVector> vectorOperation) Applies a vectorized binary operation to two strings usingShortVector.- Parameters:
a- the first stringb- the second stringoperation- the scalar operation (for fallback)vectorOperation- the vectorized operation- Returns:
- the resulting string after applying the vectorized operation
-
doApplyOperation
public static char doApplyOperation(char aChar, char bChar, BiFunction<Character, Character, Integer> operation) Applies a scalar binary operation on two characters.- Parameters:
aChar- the first characterbChar- the second characteroperation- the binary operation- Returns:
- the resulting character after applying the operation
-
charWiseAdd
Performs character-wise addition between two strings.- Parameters:
a- the first stringb- the second string- Returns:
- the resulting string
-
charWiseSubtract
Performs character-wise subtraction between two strings.- Parameters:
a- the first stringb- the second string- Returns:
- the resulting string
-
charWiseMultiply
Performs character-wise multiplication between two strings.- Parameters:
a- the first stringb- the second string- Returns:
- the resulting string
-
charWiseDivide
Performs character-wise division between two strings.- Parameters:
a- the first stringb- the second string- Returns:
- the resulting string
-
charWiseModulo
Performs character-wise modulo between two strings.- Parameters:
a- the first stringb- the second string- Returns:
- the resulting string
-
xor
Performs bitwise XOR between characters in two strings.- Parameters:
a- the first stringb- the second string- Returns:
- the resulting string
-
and
Performs bitwise AND between characters in two strings.- Parameters:
a- the first stringb- the second string- Returns:
- the resulting string
-
or
Performs bitwise OR between characters in two strings.- Parameters:
a- the first stringb- the second string- Returns:
- the resulting string
-
applyOperation
Applies a unary character-wise operation to a string.- Parameters:
a- the input stringoperation- the unary character operation- Returns:
- the resulting string
-
applyOperationScalar
Applies a scalar unary character-wise operation to a string.- Parameters:
a- the input stringoperation- the unary character operation- Returns:
- the resulting string
-
applyOperationVectorized
public static String applyOperationVectorized(String input, Function<Character, Number> scalarOperation, Function<ShortVector, ShortVector> vectorOperation) Applies a vectorized unary operation usingShortVector.- Parameters:
input- the input stringscalarOperation- the scalar fallback operationvectorOperation- the vector operation to apply- Returns:
- the resulting string
-
doApplyOperation
Applies a unary operation on a single character.- Parameters:
aChar- the input characteroperation- the unary character operation- Returns:
- the resulting character
-
not
Performs bitwise NOT operation on each character of the string.- Parameters:
a- the input string- Returns:
- the resulting string
-
preIncrement
Pre-increments each character in the string.- Parameters:
a- the input string- Returns:
- the resulting string
-
postIncrement
Post-increments each character in the string.- Parameters:
a- the input string- Returns:
- the resulting string
-
preDecrement
Pre-decrements each character in the string.- Parameters:
a- the input string- Returns:
- the resulting string
-
postDecrement
Post-decrements each character in the string.- Parameters:
a- the input string- Returns:
- the resulting string
-
stringToInt
Converts a string into an integer by summing its Unicode code points.- Parameters:
s- the input string- Returns:
- the sum of the code points
-
newNaftahInvalidEmptyInputStringCannotBeEmptyBugError
Creates aNaftahBugErrorindicating that the input string was null or empty.- Returns:
- a new
NaftahBugErrorwith a message explaining the invalid input.
-
newNaftahPartsCountMustBeGreaterThanZeroBugError
Creates aNaftahBugErrorindicating that the number of parts is zero or negative.- Returns:
- a new
NaftahBugErrorwith a message explaining the invalid part count.
-
newNaftahPartsCountExceedsStringLengthBugError
Creates aNaftahBugErrorindicating that the number of parts exceeds the string length.- Returns:
- a new
NaftahBugErrorwith a message explaining the constraint violation.
-