Class XChars


  • public final class XChars
    extends Object
    Static util class with character operation algorithms missing in or significantly superior to those in JDK.
    • Constructor Detail

      • XChars

        public XChars()
    • Method Detail

      • maxCharCount_byte

        public static final int maxCharCount_byte()
      • maxCharCount_boolean

        public static final int maxCharCount_boolean()
      • maxCharCount_short

        public static final int maxCharCount_short()
      • maxCharCount_char

        public static final int maxCharCount_char()
      • maxCharCount_int

        public static final int maxCharCount_int()
      • maxCharCount_float

        public static final int maxCharCount_float()
      • maxCharCount_long

        public static final int maxCharCount_long()
      • maxCharCount_double

        public static final int maxCharCount_double()
      • isWhitespace

        public static final boolean isWhitespace​(char c)
      • isNonWhitespace

        public static final boolean isNonWhitespace​(char c)
      • standardCharset

        public static final Charset standardCharset()
        Returns StandardCharsets.UTF_8, because any other one out there is nonsense as a standard.
        Returns:
        java.nio.charset.Charset.forName("UTF-8").
      • validateIndex

        public static final void validateIndex​(char[] chars,
                                               int index)
      • validateRange

        public static final void validateRange​(char[] chars,
                                               int offset,
                                               int length)
      • isEqual

        public static final boolean isEqual​(String s1,
                                            String s2)
      • equals

        public static final boolean equals​(String string,
                                           char[] chars,
                                           int offset)
      • equals

        public static final boolean equals​(char[] chars1,
                                           int offset1,
                                           char[] chars2,
                                           int offset2,
                                           int length)
      • isEqualIgnoreCase

        public static final boolean isEqualIgnoreCase​(String s1,
                                                      String s2)
      • indexOf

        public static final int indexOf​(char[] data,
                                        char c)
      • indexOf

        public static final int indexOf​(char[] data,
                                        int offset,
                                        int length,
                                        char c)
      • indexOf

        public static final int indexOf​(char[] source,
                                        int sourceOffset,
                                        int sourceCount,
                                        char[] target,
                                        int targetOffset,
                                        int targetCount,
                                        int fromIndex)
      • indexOf

        public static final int indexOf​(char[] data,
                                        int dataLength,
                                        char[] subject)
      • indexOf

        public static final int indexOf​(char[] data,
                                        int dataOffset,
                                        int dataLength,
                                        char[] chars)
      • indexOf

        public static final int indexOf​(char[] source,
                                        int sourceCount,
                                        char[] target,
                                        int targetCount,
                                        int fromIndex)
      • replaceFirst

        public static final int replaceFirst​(char[] chars,
                                             int offset,
                                             int length,
                                             char sample,
                                             char replacement)
      • count

        public static final int count​(char[] haystack,
                                      int haystackOffset,
                                      int haystackCount,
                                      char needle)
      • count

        public static final int count​(char[] data,
                                      int dataOffset,
                                      int dataCount,
                                      char[] subject,
                                      int subjectOffset,
                                      int subjectCount)
      • hasContent

        public static final boolean hasContent​(CharSequence s)
      • hasNoContent

        public static final boolean hasNoContent​(CharSequence s)
      • hasContent

        public static final boolean hasContent​(String s)
      • hasNoContent

        public static final boolean hasNoContent​(String s)
      • asString

        public static final String asString​(char c)
        This method does intentionally not use the pseudo-constructor naming pattern since it does not create new instances of String. Instead, it returns a constant instance of an intrinsic look-up table.
        Parameters:
        c - the character value to be represented as a String.
        Returns:
        a String containing the passed character value.
      • asString

        public static final String asString​(char... chars)
      • upperCaseFirstChar

        public static final String upperCaseFirstChar​(String s)
        Ensures that the first character of the passed String is in upper case.

        If the passed String is null, has a length of 0 or already has an upper case first character, it is returned. Otherwise, the first character of the passed String is transformed to upper case and concatenated with the rest of the passed String into a new String instance.

        Parameters:
        s - the String for which the first character shall be ensured to be upper case.
        Returns:
        a string equalling s with its first character being guaranteed to be upper case.
      • lowerCaseFirstChar

        public static final String lowerCaseFirstChar​(String s)
      • concat

        public static final char[] concat​(char[]... arrays)
      • appendArraySeperated

        public static final VarString appendArraySeperated​(VarString vc,
                                                           char separator,
                                                           Object... elements)
      • toCharArray

        public static final char[] toCharArray​(CharSequence c)
      • toCharArray

        public static final char[] toCharArray​(StringBuilder asb)
      • toCharArray

        public static final char[] toCharArray​(StringBuffer asb)
      • intersects

        public static final boolean intersects​(char[] chars1,
                                               char[] chars2)
        Returns true if the two given character arrays have at least one character in common.
        Parameters:
        chars1 -
        chars2 -
        Returns:
        true if the two given character arrays intersect each other.
      • append

        public static final char[] append​(char c,
                                          char[] string)
      • append

        public static final char[] append​(char[] string,
                                          char c)
      • append

        public static final char[] append​(char[] a,
                                          char[] b)
      • longestCommonSubstringLength

        public static final int longestCommonSubstringLength​(String a,
                                                             String b)
      • commonSubstringLength

        public static final int commonSubstringLength​(char[] a,
                                                      char[] b)
      • longestCommonSubstring

        public static final String longestCommonSubstring​(String a,
                                                          String b)
      • longestCommonSubstring

        public static final char[] longestCommonSubstring​(char[] a,
                                                          char[] b)
      • substring

        public static final char[] substring​(char[] string,
                                             int offset,
                                             int bound)
      • convertUnderscoresToCamelCase

        public static final String convertUnderscoresToCamelCase​(String parameterName)
      • convertUnderscoresToCamelCase

        public static final VarString convertUnderscoresToCamelCase​(VarString vs,
                                                                    String s)
      • longestCommonPrefixLength

        public static final int longestCommonPrefixLength​(String a,
                                                          String b)
      • longestCommonSuffixLength

        public static final int longestCommonSuffixLength​(String a,
                                                          String b)
      • longestCommonSuffix

        public static final String longestCommonSuffix​(String a,
                                                       String b)
      • longestCommonPrefix

        public static final String longestCommonPrefix​(String a,
                                                       String b)
      • commonPrefixLength

        public static final int commonPrefixLength​(char[] a,
                                                   char[] b)
      • commonSuffixLength

        public static final int commonSuffixLength​(char[] a,
                                                   char[] b)
      • longestCommonSuffix

        public static final char[] longestCommonSuffix​(char[] a,
                                                       char[] b)
      • longestCommonPrefix

        public static final char[] longestCommonPrefix​(char[] a,
                                                       char[] b)
      • padLeft

        public static final String padLeft​(String s,
                                           int totalLength,
                                           char paddingChar)
      • padRight

        public static final String padRight​(String s,
                                            int totalLength,
                                            char paddingChar)
      • padLeft

        public static final String padLeft​(String s,
                                           int totalLength)
      • padRight

        public static final String padRight​(String s,
                                            int totalLength)
      • padLeft0

        public static final String padLeft0​(String s,
                                            int totalLength)
      • padRight0

        public static final String padRight0​(String s,
                                             int totalLength)
      • parseChar

        public static final char parseChar​(String s)
        Parses the char escape sequence Strings "\n" etc. (wihtout the "") to the singel char value represented by those strings.
        Parameters:
        s -
      • escapeChar

        public static final String escapeChar​(char c)
      • createMedialCapitalsString

        public static final String createMedialCapitalsString​(String... elements)
      • ensureCharAtEnd

        public static final String ensureCharAtEnd​(String s,
                                                   char c)
      • toSingleLine

        public static final String toSingleLine​(String multiLineString,
                                                String lineBreakReplacement)
      • padSpace

        public static final String padSpace​(String s,
                                            int chars)
      • concat

        public static final String concat​(char separator,
                                          Object... parts)
      • notEmpty

        public static final <C extends CharSequence> C notEmpty​(C charSequence)
      • isEmpty

        public static final boolean isEmpty​(CharSequence cs)
      • iterate

        public static final void iterate​(char[] chars,
                                         _charProcedure iterator)
      • iterate

        public static final void iterate​(char[] chars,
                                         int offset,
                                         int length,
                                         _charProcedure iterator)
      • put

        public static final int put​(byte value,
                                    char[] target,
                                    int offset)
      • putHexDec

        public static final int putHexDec​(byte value,
                                          char[] target,
                                          int offset)
      • put

        public static final int put​(boolean value,
                                    char[] target,
                                    int offset)
      • put

        public static final int put​(short value,
                                    char[] target,
                                    int offset)
      • put

        public static final int put​(int value,
                                    char[] target,
                                    int offset)
      • put

        public static final int put​(float value,
                                    char[] target,
                                    int offset)
      • put

        public static final int put​(long value,
                                    char[] target,
                                    int offset)
      • put

        public static final int put​(double value,
                                    char[] target,
                                    int offset)
      • put

        public static final int put​(String s,
                                    char[] target,
                                    int offset)
      • put

        public static final int put​(char[] value,
                                    char[] target,
                                    int offset)
      • putNull

        public static final int putNull​(char[] target,
                                        int offset)
      • putTrue

        public static final int putTrue​(char[] target,
                                        int offset)
      • putFalse

        public static final int putFalse​(char[] target,
                                         int offset)
      • String

        public static final String String​(byte value)
      • String

        public static final String String​(boolean value)
      • String

        public static final String String​(short value)
      • String

        public static final String String​(char value)
      • String

        public static final String String​(int value)
      • String

        public static final String String​(float value)
      • String

        public static final String String​(long value)
      • String

        public static final String String​(double value)
      • String

        public static final String String​(char[] chars)
      • String

        public static final String String​(char[] chars,
                                          int offset,
                                          int length)
      • String

        public static final String String​(byte[] chars)
      • String

        public static final String String​(byte[] chars,
                                          Charset charset)
      • String

        public static final String String​(byte[] chars,
                                          int offset,
                                          int length)
      • String

        public static final String String​(byte[] chars,
                                          int offset,
                                          int length,
                                          Charset charset)
      • String

        public static final String String​(ByteBuffer chars,
                                          int offset,
                                          int length)
      • StringBuffer

        public static final StringBuffer StringBuffer​(VarString vs,
                                                      int offset,
                                                      int length)
      • readChars

        public static final char[] readChars​(String s)
      • hashCode

        public static final int hashCode​(char[] chars,
                                         int offset,
                                         int length)
        This method rebuilds the String hashing algorithm as the JDK guys forgot as usual to make their stuff professionally modular.

        Returns a hash code for the passed character range. The hash code is computed as

         s[0]*31^(n - 1) + s[1]*31^(n-2) + ... + s[n - 1]
         
        using int arithmetic, where s[i] is the ith character counted from the given offset, n is the length and ^ indicates exponentiation. The hash value of an empty range is zero.
        Returns:
        a hash code value for this object.
        See Also:
        String.hashCode()
      • splitAndTrimToStrings

        public static final <C extends Consumer<String>> C splitAndTrimToStrings​(String input,
                                                                                 char separator,
                                                                                 C collector)
        High-performance implementation of the very common case to split a string by a single character and trim all elements.
        Parameters:
        input -
        separator -
        See Also:
        trimToString(char[],int,int)
      • splitAndTrimToStrings

        public static final <C extends Consumer<String>> C splitAndTrimToStrings​(char[] input,
                                                                                 char separator,
                                                                                 C collector)
        High-performance implementation of the very common case to split a character sequence by a single character and trim all elements.
        Parameters:
        input -
        separator -
        See Also:
        trimToString(char[],int,int)
      • trimToString

        public static final String trimToString​(char[] input,
                                                int lowerOffset,
                                                int length)
        Creates a String instance with trimmed content directly from a character sequence without unnecessary intermediate instances.
        Parameters:
        input -
        lowerOffset -
        length -
      • trim

        public static String trim​(String s)
        Calls String.trim() on a non-null argument, returns null otherwise. (this is nothing but a static String.trim())
        Parameters:
        s - the String instance to be trimmed, potentially null.
        Returns:
        a potentially null trimmed String instance.
      • parseTo_intArray

        public static final int[] parseTo_intArray​(String... intStrings)
      • parseToIntegerArray

        public static final Integer[] parseToIntegerArray​(String... intStrings)
      • assembleNewLinedTabbed

        public static final void assembleNewLinedTabbed​(VarString vs,
                                                        CharSequence... elements)
      • parse_byteDecimal

        public static final byte parse_byteDecimal​(char[] input)
      • parse_shortDecimal

        public static final short parse_shortDecimal​(char[] input)
      • parse_intLiteral

        public static final int parse_intLiteral​(char[] input)
      • parse_longDecimal

        public static final long parse_longDecimal​(char[] input)
      • parse_byteDecimal

        public static final byte parse_byteDecimal​(char[] input,
                                                   int offset,
                                                   int length)
      • parse_shortDecimal

        public static final short parse_shortDecimal​(char[] input,
                                                     int offset,
                                                     int length)
      • parse_intLiteral

        public static final int parse_intLiteral​(char[] input,
                                                 int offset,
                                                 int length)
      • parse_longDecimal

        public static final long parse_longDecimal​(char[] input,
                                                   int offset,
                                                   int length)
      • parse_float

        public static final float parse_float​(char[] input,
                                              int offset,
                                              int length)
      • parse_double

        public static final double parse_double​(char[] input,
                                                int offset,
                                                int length)
      • uncheckedParse_byteDecimal

        public static final byte uncheckedParse_byteDecimal​(char[] input,
                                                            int offset,
                                                            int length)
      • uncheckedParse_shortDecimal

        public static final short uncheckedParse_shortDecimal​(char[] input,
                                                              int offset,
                                                              int length)
      • uncheckedParse_intLiteral

        public static final int uncheckedParse_intLiteral​(char[] input,
                                                          int offset,
                                                          int length)
      • uncheckedParse_longLiteral

        public static final long uncheckedParse_longLiteral​(char[] input,
                                                            int offset,
                                                            int length)
      • internalParse_longLiteral

        public static final long internalParse_longLiteral​(char[] input,
                                                           int offset,
                                                           int length)
        Specialcase higher performance implementation of decimal integer literal parsing. Because as usual, the JDK implementation strategies are not acceptable when dealing with non-trivial amounts of data. Properly executed performance tests (large loop sizes, averages, nanosecond precision, etc.) showed that this algorithms is more than twice as fast as the one used in JDK (average of ~33µs vs ~75µs for long literals on same machine with measuring overhead of ~1.5µs)
        Parameters:
        input -
        offset -
        length -
      • to_int

        public static final int to_int​(char digit)
      • contains

        public static final boolean contains​(String[] strings,
                                             String subject)
      • contains

        public static final boolean contains​(char[] subject,
                                             int subjectOffset,
                                             int subjectLength,
                                             char[] sample,
                                             int sampleOffset,
                                             int sampleLength)
      • systemString

        public static String systemString​(Object instance)
        Utility method that replicates the JVM's intrinsic system string as defined in Object.toString(). (It's funny how much functionality is missing in the JDK API).
        Parameters:
        instance - the instance whose system string shall be generated.
        Returns:
        the system string for the passed instance.
      • nonNullString

        public static String nonNullString​(Object object)
      • valueString

        public static String valueString​(Object value)
        Returns value.toString() if the passed value is not null, otherwise null.

        Note that this is a different behavior than String.valueOf(Object) has, as the latter returns the string "null" for a passed null reference. The latter is merely an output helper method, albeit clumsily named and placed as if it were a general utility method. THIS method here is the far more useful general utility method.

        The behavior of this method is needed for example for converting values in a generic data structure (e.g. a Object[] array) to string values but have the actual values, including null(information about a missing value), maintained.

        Parameters:
        value - the value to be projected to its string representation if not null.
        Returns:
        a string representation of an actual passed value or a transient null.
        See Also:
        Object.toString(), String.valueOf(Object)
      • trimEmptyToNull

        public static String trimEmptyToNull​(String s)
      • isDigit

        public static boolean isDigit​(char c)
      • randomReadable_char

        public static char randomReadable_char()