Skip to content

io_nats_jparse_source_support

Rick Hightower edited this page Jul 18, 2023 · 1 revision

io.nats.jparse.source.support

class diagram

ParseFloat

The ParseFloat class is a public class that is used for parsing floating-point values.

public static float parseFloat(char[] chars, int startIndex, int endIndex)

public static float parseFloat(char[] chars, int startIndex, int endIndex) {
    boolean negative = false;
    int i = startIndex;
    float result = 0;
    // Check for a negative sign
    if (chars[i] == '-') {
        negative = true;
        i++;
    }
    loop: while (i < endIndex) {
        char ch = chars[i];
        switch(ch) {
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
                result = result * 10 + (ch - '0');
                i++;
                break;
            case '.':
                result = parseFractionPart(i + 1, endIndex, chars, result);
                break loop;
            case 'e':
                result = parseExponent(i + 1, endIndex, chars, result);
                break loop;
            default:
                throw new UnexpectedCharacterException("parsing float", "Illegal character", ch, i);
        }
    }
    if (negative) {
        result = -result;
    }
    return result;
}

The parseFloat method in the io.nats.jparse.source.support.ParseFloat class is a static method that takes three parameters: char[] chars, int startIndex, and int endIndex. It returns a float value.

Here is a step-by-step description of what the parseFloat method does based on its body:

  1. It initializes a boolean variable negative to false, an integer variable i to the value of startIndex, and a float variable result to 0.

  2. It checks if the character at index i in the chars array is '-'. If it is, it sets negativetotrueand incrementsi` by 1.

  3. It enters a loop that continues until i is less than endIndex.

  4. Inside the loop, it assigns the character at index i in the chars array to a char variable ch.

  5. It checks the value of ch using a switch statement. If ch is a digit from 0 to 9, it performs the following steps:

    • Multiplies the result by 10 and adds the numeric value of ch minus the character '0' (to convert from ASCII to the actual numeric value).
    • Increments i by 1.
    • Breaks out of the switch statement.
  6. If ch is a '.', it calls a method parseFractionPart with parameters i + 1, endIndex, chars, and result and assigns the return value to result.

  7. If ch is an 'e', it calls a method parseExponent with parameters i + 1, endIndex, chars, and result and assigns the return value to result.

  8. If ch does not match any of the cases in the switch statement, it throws an UnexpectedCharacterException with the message "Illegal character" and the values of "parsing float", the actual character ch, and the index i.

  9. After the loop ends, it checks if negative is true. If it is, it negates the value of result.

  10. Finally, it returns the value of result.

Note: The parseFractionPart and parseExponent methods are not included in the given code, so their behavior is not described here. sequence diagram

private static float parseFractionPart(int i, int endIndex, char[] chars, float result)

private static float parseFractionPart(int i, int endIndex, char[] chars, float result) {
    float fraction = 0.1f;
    while (i < endIndex) {
        char ch = chars[i];
        switch(ch) {
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
                result += (ch - '0') * fraction;
                fraction /= 10;
                i++;
                break;
            case 'e':
                return parseExponent(i + 1, endIndex, chars, result);
            default:
                throw new UnexpectedCharacterException("float parsing fraction part", "Illegal character", ch, i);
        }
    }
    return result;
}

The parseFractionPart method, defined in the ParseFloat class of the io.nats.jparse.source.support package, is used to parse and calculate the fraction part of a floating-point number.

Here is a step-by-step description of what the method is doing:

  1. The method takes the following parameters: i (the starting index of the fraction part), endIndex (the index at which the fraction part ends), chars (an array of characters representing the input number), and result (the current result of the parsing operation).

  2. It initializes a variable fraction as 0.1.

  3. It enters a while loop that iterates from i to endIndex.

  4. Within the loop, the method retrieves the character at the current index (i) from the chars array and assigns it to a variable ch.

  5. It uses a switch statement to perform different actions based on the character ch.

  6. If the character ch is a digit ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9'), the method calculates the corresponding fractional value and updates the result accordingly. It multiplies the difference between ch and '0' by fraction and adds it to the result. It then divides fraction by 10 to handle the next digit and increments i to move to the next character.

  7. If the character ch is 'e', the method returns the result of another method called parseExponent (which is not defined in the given code snippet).

  8. If the character ch is neither a digit nor 'e', the method throws an UnexpectedCharacterException with an appropriate error message.

  9. After each iteration of the loop, i is incremented to process the next character.

  10. Once the loop completes, the method returns the final result.

Note: The given code snippet only includes the implementation of the parseFractionPart method. It is assumed that the parseExponent method is defined elsewhere in the code. sequence diagram

private static float parseExponent(int i, int endIndex, char[] chars, float result)

private static float parseExponent(int i, int endIndex, char[] chars, float result) {
    boolean exponentNegative = false;
    int exponent = 0;
    char sign = chars[i];
    switch(sign) {
        case '-':
            exponentNegative = true;
            i++;
            break;
        case '+':
            i++;
            break;
    }
    while (i < endIndex) {
        char ch = chars[i];
        switch(chars[i]) {
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
                exponent = exponent * 10 + (ch - '0');
                i++;
                break;
            default:
                throw new UnexpectedCharacterException("float parsing exponent part", "Illegal character", ch, i);
        }
    }
    if (exponentNegative) {
        exponent = -exponent;
    }
    // Use Lookup table for powers of 10
    // Calculate the power of 10
    if (!exponentNegative) {
        while (exponent >= powersOf10.length) {
            result *= 1e18f;
            exponent -= 18;
        }
        result *= powersOf10[exponent];
    } else {
        while (-exponent >= powersOf10.length) {
            result /= 1e18f;
            exponent += 18;
        }
        result /= powersOf10[-exponent];
    }
    return result;
}

The method parseExponent is used to parse the exponent part of a floating-point number. Here is a step-by-step description of what the method is doing based on its body:

  1. It accepts four parameters: i (the starting index of the exponent part), endIndex (the ending index of the exponent part), chars (the array of characters representing the number), and result (the current result of parsing the float value).

  2. It initializes a boolean variable exponentNegative to false, which will indicate if the exponent is negative.

  3. It initializes an integer variable exponent to store the parsed exponent value.

  4. It retrieves the character at the index i from the chars array and assigns it to the variable sign.

  5. It checks the value of sign using a switch statement:

    • If sign is '-' (indicating a negative exponent), it sets exponentNegative to true and increments i by 1.
    • If sign is '+' (indicating a positive exponent), it increments i by 1.
  6. It enters a while loop that iterates while the index i is less than endIndex.

  7. Inside the loop, it retrieves the character at the index i from the chars array and assigns it to the variable ch.

  8. It checks the value of ch using a switch statement:

    • If ch is a digit from '0' to '9', it converts the character to its corresponding integer value and adds it to the exponent variable. It then increments i by 1.
    • If ch is not a digit, it throws an UnexpectedCharacterException with an appropriate error message indicating that an illegal character was encountered during parsing.
  9. After the loop, it checks if exponentNegative is true. If so, it negates the value of exponent.

  10. It enters an if-else condition to calculate the power of 10 based on the value of exponent and update the result accordingly:

    • If exponentNegative is false (indicating a positive exponent), it enters a while loop that continues while exponent is greater than or equal to powersOf10.length. Inside the loop, it multiplies result by 1e18f (a constant representing 10^18) and subtracts 18 from exponent. Finally, it multiplies result by the power of 10 stored in the powersOf10 array at the index exponent.
    • If exponentNegative is true (indicating a negative exponent), it enters a while loop that continues while the negative value of exponent is greater than or equal to powersOf10.length. Inside the loop, it divides result by 1e18f and adds 18 to the negative exponent. Finally, it divides result by the power of 10 stored in the powersOf10 array at the index -exponent.
  11. Finally, it returns the updated result.

Note: The method assumes the existence of a powersOf10 array, which is not defined in the given code snippet. sequence diagram

UnexpectedCharacterException

This class represents an exception that is thrown when an unexpected character is encountered during parsing. It is used in cases where an unexpected character is encountered while parsing a JSON string or CharSource. This exception can be thrown by the JsonParser class in the io.nats.jparse.parser package. For more information, refer to the CharSource class and the JsonParser class.

PathException

The PathException class is a runtime exception that is used to handle errors related to JSON paths. It is thrown when an unexpected condition is encountered while processing a path.

ParseDouble

The ParseDouble class is a class that is used to parse and convert string representations of numbers into the double data type.

public static double parseDouble(char[] chars, int startIndex, int endIndex)

public static double parseDouble(char[] chars, int startIndex, int endIndex) {
    boolean negative = false;
    int i = startIndex;
    double result = 0;
    // Check for a negative sign
    if (chars[i] == '-') {
        negative = true;
        i++;
    }
    loop: while (i < endIndex) {
        char ch = chars[i];
        switch(ch) {
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
                result = result * 10 + (ch - '0');
                i++;
                break;
            case '.':
                result = parseFractionPart(i + 1, endIndex, chars, result);
                break loop;
            case 'E':
            case 'e':
                result = parseExponent(i + 1, endIndex, chars, result);
                break loop;
            default:
                throw new UnexpectedCharacterException("parsing double", "Illegal character", ch, i);
        }
    }
    if (negative) {
        result = -result;
    }
    return result;
}

The parseDouble method defined in the ParseDouble class in the io.nats.jparse.source.support package is used to parse a double value from a character array.

Here is a step-by-step description of what the method does based on its body:

  1. Initialize a boolean variable negative to false.
  2. Initialize an integer variable i to the startIndex parameter.
  3. Initialize a double variable result to 0.
  4. Check if the character at index i in the chars array is a negative sign ('-'). If it is, set negative to true and increment i by 1.
  5. Enter a loop that iterates while i is less than endIndex.
  6. Get the character at index i in the chars array and assign it to the variable ch.
  7. Use a switch statement to check the value of ch:
    • If ch is a digit ('0' to '9'), multiply result by 10 and add the numerical value of ch (obtained by subtracting the character '0') to it. Then, increment i by 1.
    • If ch is a period ('.'), call the parseFractionPart method with the parameters i + 1, endIndex, chars, and result. Assign the returned value to result, and break the loop using the break loop statement.
    • If ch is an 'E' or 'e', call the parseExponent method with the parameters i + 1, endIndex, chars, and result. Assign the returned value to result, and break the loop using the break loop statement.
    • If ch is none of the above, throw an UnexpectedCharacterException with the appropriate message, character, and position.
  8. If negative is true, negate result.
  9. Return the value of result.

private static double parseFractionPart(int i, int endIndex, char[] chars, double result)

private static double parseFractionPart(int i, int endIndex, char[] chars, double result) {
    double fraction = 0.1;
    while (i < endIndex) {
        char ch = chars[i];
        switch(ch) {
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
                result += (ch - '0') * fraction;
                fraction /= 10;
                i++;
                break;
            case 'E':
            case 'e':
                return parseExponent(i + 1, endIndex, chars, result);
            default:
                throw new UnexpectedCharacterException("double parsing fraction part", "Illegal character", ch, i);
        }
    }
    return result;
}

Method: parseFractionPart

Class: io.nats.jparse.source.support.ParseDouble

Description: This method is used to parse the fraction part of a double number represented as a character array. It iterates through each character in the fraction part, calculates the numeric value of the fraction, and updates the overall result.

Parameters:

  • i (int): The current index of the character being processed in the fraction part.
  • endIndex (int): The index of the last character in the fraction part.
  • chars (char[]): The character array containing the fraction part of the double number.
  • result (double): The accumulated result of parsing the fraction part.

Returns:

  • double: The final result after parsing the fraction part.

Steps:

  1. Initialize the fraction variable to 0.1.
  2. Start a while loop with the condition i < endIndex.
  3. Fetch the character at the current index from the character array and store it in the variable ch.
  4. Use a switch statement to perform actions based on the value of ch:
    • If ch is a digit (0-9), perform the following steps:
      • Convert the character to its numeric value by subtracting '0' from it.
      • Multiply the numeric value by the fraction variable.
      • Add the result to the updated numeric value.
      • Divide the fraction variable by 10 to decrease its value.
      • Increment i by 1 to move to the next character.
      • Break out of the switch statement.
    • If ch is 'E' or 'e', return the result of calling the parseExponent method with the appropriate parameters.
    • If ch is neither a digit nor 'E' or 'e', throw an UnexpectedCharacterException with an appropriate error message.
  5. After the while loop ends, return the final result.

Please let me know if you need more information. sequence diagram

private static double parseExponent(int i, int endIndex, char[] chars, double result)

private static double parseExponent(int i, int endIndex, char[] chars, double result) {
    boolean exponentNegative = false;
    int exponent = 0;
    char sign = chars[i];
    switch(sign) {
        case '-':
            exponentNegative = true;
            i++;
            break;
        case '+':
            i++;
            break;
    }
    while (i < endIndex) {
        char ch = chars[i];
        switch(chars[i]) {
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
                exponent = exponent * 10 + (ch - '0');
                i++;
                break;
            default:
                throw new UnexpectedCharacterException("double parsing parsing exponent", "Illegal character", ch, i);
        }
    }
    if (exponentNegative) {
        exponent = -exponent;
    }
    // Use Lookup table for powers of 10
    // Calculate the power of 10
    if (!exponentNegative) {
        while (exponent >= powersOf10.length) {
            result *= 1e22;
            exponent -= 22;
        }
        result *= powersOf10[exponent];
    } else {
        while (-exponent >= powersOf10.length) {
            result /= 1e22;
            exponent += 22;
        }
        result /= powersOf10[-exponent];
    }
    return result;
}

The parseExponent method in class io.nats.jparse.source.support.ParseDouble is used to parse the exponent part of a floating-point number represented as a char array.

Here is a step-by-step description of what the method does:

  1. The method takes in four parameters: i (the current index of the char array), endIndex (the index of the last character in the char array), chars (the char array containing the number), and result (the parsed value of the number so far).

  2. The method initializes a boolean variable exponentNegative to false and an integer variable exponent to 0. It also stores the value of the i-th character in the chars array in a variable sign.

  3. The method then enters a switch statement based on the value of sign: a. If sign is '-', the exponentNegative variable is set to true and the value of i is incremented. b. If sign is '+', the value of i is incremented.

  4. The method then enters a while loop that continues as long as i is less than endIndex.

  5. Inside the loop, the method retrieves the character at the i-th index of the chars array and enters a switch statement based on its value. a. If the character is a digit (0-9), the value of exponent is updated by multiplying it by 10 and adding the difference between the character value and the character value of '0'. The value of i is also incremented. b. If the character is not a digit, an UnexpectedCharacterException is thrown with an appropriate error message.

  6. After the while loop, the method checks if exponentNegative is true. If it is, the value of exponent is negated.

  7. The method then enters an if statement to handle the calculation of the power of 10. a. If exponentNegative is false, a while loop is initiated that runs as long as exponent is greater than or equal to the length of powersOf10 (presumably an array storing pre-calculated powers of 10).

    • Inside the loop, result is multiplied by 1e22 (which is 10 raised to the power 22) and exponent is decreased by 22. b. Finally, result is multiplied by the power of 10 corresponding to exponent.
  8. If exponentNegative is true, a similar while loop is initiated but with negated exponent and division instead of multiplication.

  9. Finally, the method returns the result.

Note: The code snippet provided doesn't include the definition of the powersOf10 array, so it's assumed that it's defined somewhere else in the code. sequence diagram

CharArraySegment

The CharArraySegment class is a public class that implements the CharSequence interface. It represents a segment of a character array (char[]), and provides access to a subsequence of the array as a CharSequence.