Class StringUtils

java.lang.Object
org.daiitech.naftah.builtin.utils.StringUtils

public final class StringUtils extends Object
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 Details

  • Constructor Details

    • StringUtils

      private StringUtils()
      Private constructor to prevent instantiation. Always throws a NaftahBugError when called.
  • Method Details

    • equals

      public static boolean equals(String a, String b)
      Checks if two strings are equal.
      Parameters:
      a - the first string
      b - the second string
      Returns:
      true if both strings are equal
    • compare

      public static int compare(String a, String b)
      Compares two strings lexicographically.
      Parameters:
      a - the first string
      b - the second string
      Returns:
      a negative integer, zero, or a positive integer as a is less than, equal to, or greater than b
    • add

      public static String add(String a, String b)
      Concatenates two strings.
      Parameters:
      a - the first string
      b - the second string
      Returns:
      the concatenated string
    • subtract

      public static String subtract(String a, String b)
      Removes all occurrences of b from a.
      Parameters:
      a - the original string
      b - the substring to remove
      Returns:
      the resulting string
    • divide

      public static String[] divide(String a, String delimiter)
      Splits a string using the given delimiter.
      Parameters:
      a - the string to split
      delimiter - the delimiter
      Returns:
      an array of substrings
    • divide

      public static String[] divide(String s, int parts)
      Splits a string into a specified number of parts.
      Parameters:
      s - the string to split
      parts - the number of parts
      Returns:
      an array containing the parts
      Throws:
      NaftahBugError - if the string is empty, or if parts is invalid
    • multiply

      public static String multiply(String s, int multiplier)
      Repeats the given string a specified number of times.
      Parameters:
      s - the input string
      multiplier - 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 a or b is null, throws a NaftahBugError indicating 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 be null)
      b - the second string (must not be null)
      operation - the character-wise binary operation (e.g. addition, subtraction)
      Returns:
      the resulting string after applying the operation
      Throws:
      NaftahBugError - if either a or b is null
    • 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 string
      b - the second string
      operation - 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 using ShortVector.
      Parameters:
      a - the first string
      b - the second string
      operation - 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 character
      bChar - the second character
      operation - the binary operation
      Returns:
      the resulting character after applying the operation
    • charWiseAdd

      public static String charWiseAdd(String a, String b)
      Performs character-wise addition between two strings.
      Parameters:
      a - the first string
      b - the second string
      Returns:
      the resulting string
    • charWiseSubtract

      public static String charWiseSubtract(String a, String b)
      Performs character-wise subtraction between two strings.
      Parameters:
      a - the first string
      b - the second string
      Returns:
      the resulting string
    • charWiseMultiply

      public static String charWiseMultiply(String a, String b)
      Performs character-wise multiplication between two strings.
      Parameters:
      a - the first string
      b - the second string
      Returns:
      the resulting string
    • charWiseDivide

      public static String charWiseDivide(String a, String b)
      Performs character-wise division between two strings.
      Parameters:
      a - the first string
      b - the second string
      Returns:
      the resulting string
    • charWiseModulo

      public static String charWiseModulo(String a, String b)
      Performs character-wise modulo between two strings.
      Parameters:
      a - the first string
      b - the second string
      Returns:
      the resulting string
    • xor

      public static String xor(String a, String b)
      Performs bitwise XOR between characters in two strings.
      Parameters:
      a - the first string
      b - the second string
      Returns:
      the resulting string
    • and

      public static String and(String a, String b)
      Performs bitwise AND between characters in two strings.
      Parameters:
      a - the first string
      b - the second string
      Returns:
      the resulting string
    • or

      public static String or(String a, String b)
      Performs bitwise OR between characters in two strings.
      Parameters:
      a - the first string
      b - the second string
      Returns:
      the resulting string
    • applyOperation

      public static String applyOperation(String a, Function<Character,Number> operation)
      Applies a unary character-wise operation to a string.
      Parameters:
      a - the input string
      operation - the unary character operation
      Returns:
      the resulting string
    • applyOperationScalar

      public static String applyOperationScalar(String a, Function<Character,Number> operation)
      Applies a scalar unary character-wise operation to a string.
      Parameters:
      a - the input string
      operation - 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 using ShortVector.
      Parameters:
      input - the input string
      scalarOperation - the scalar fallback operation
      vectorOperation - the vector operation to apply
      Returns:
      the resulting string
    • doApplyOperation

      public static char doApplyOperation(char aChar, Function<Character,Number> operation)
      Applies a unary operation on a single character.
      Parameters:
      aChar - the input character
      operation - the unary character operation
      Returns:
      the resulting character
    • not

      public static String not(String a)
      Performs bitwise NOT operation on each character of the string.
      Parameters:
      a - the input string
      Returns:
      the resulting string
    • preIncrement

      public static String preIncrement(String a)
      Pre-increments each character in the string.
      Parameters:
      a - the input string
      Returns:
      the resulting string
    • postIncrement

      public static String postIncrement(String a)
      Post-increments each character in the string.
      Parameters:
      a - the input string
      Returns:
      the resulting string
    • preDecrement

      public static String preDecrement(String a)
      Pre-decrements each character in the string.
      Parameters:
      a - the input string
      Returns:
      the resulting string
    • postDecrement

      public static String postDecrement(String a)
      Post-decrements each character in the string.
      Parameters:
      a - the input string
      Returns:
      the resulting string
    • stringToInt

      public static int stringToInt(String s)
      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

      public static NaftahBugError newNaftahInvalidEmptyInputStringCannotBeEmptyBugError()
      Creates a NaftahBugError indicating that the input string was null or empty.
      Returns:
      a new NaftahBugError with a message explaining the invalid input.
    • newNaftahPartsCountMustBeGreaterThanZeroBugError

      public static NaftahBugError newNaftahPartsCountMustBeGreaterThanZeroBugError()
      Creates a NaftahBugError indicating that the number of parts is zero or negative.
      Returns:
      a new NaftahBugError with a message explaining the invalid part count.
    • newNaftahPartsCountExceedsStringLengthBugError

      public static NaftahBugError newNaftahPartsCountExceedsStringLengthBugError()
      Creates a NaftahBugError indicating that the number of parts exceeds the string length.
      Returns:
      a new NaftahBugError with a message explaining the constraint violation.