-
Notifications
You must be signed in to change notification settings - Fork 0
io_nats_jparse_source_support

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) {
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:
-
It initializes a boolean variable
negativetofalse, an integer variableito the value ofstartIndex, and a float variableresultto0. -
It checks if the character at index
iin thecharsarray is'-'. If it is, it setsnegativetotrueand incrementsi` by 1. -
It enters a loop that continues until
iis less thanendIndex. -
Inside the loop, it assigns the character at index
iin thecharsarray to a char variablech. -
It checks the value of
chusing a switch statement. Ifchis a digit from 0 to 9, it performs the following steps:- Multiplies the
resultby 10 and adds the numeric value ofchminus the character '0' (to convert from ASCII to the actual numeric value). - Increments
iby 1. - Breaks out of the switch statement.
- Multiplies the
-
If
chis a '.', it calls a methodparseFractionPartwith parametersi + 1,endIndex,chars, andresultand assigns the return value toresult. -
If
chis an 'e', it calls a methodparseExponentwith parametersi + 1,endIndex,chars, andresultand assigns the return value toresult. -
If
chdoes not match any of the cases in the switch statement, it throws anUnexpectedCharacterExceptionwith the message "Illegal character" and the values of "parsing float", the actual characterch, and the indexi. -
After the loop ends, it checks if
negativeistrue. If it is, it negates the value ofresult. -
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.

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:
-
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), andresult(the current result of the parsing operation). -
It initializes a variable
fractionas 0.1. -
It enters a
whileloop that iterates fromitoendIndex. -
Within the loop, the method retrieves the character at the current index (
i) from thecharsarray and assigns it to a variablech. -
It uses a
switchstatement to perform different actions based on the characterch. -
If the character
chis a digit ('0','1','2','3','4','5','6','7','8','9'), the method calculates the corresponding fractional value and updates theresultaccordingly. It multiplies the difference betweenchand'0'byfractionand adds it to theresult. It then dividesfractionby 10 to handle the next digit and incrementsito move to the next character. -
If the character
chis'e', the method returns the result of another method calledparseExponent(which is not defined in the given code snippet). -
If the character
chis neither a digit nor'e', the method throws anUnexpectedCharacterExceptionwith an appropriate error message. -
After each iteration of the loop,
iis incremented to process the next character. -
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.

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:
-
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), andresult(the current result of parsing the float value). -
It initializes a boolean variable
exponentNegativeto false, which will indicate if the exponent is negative. -
It initializes an integer variable
exponentto store the parsed exponent value. -
It retrieves the character at the index
ifrom thecharsarray and assigns it to the variablesign. -
It checks the value of
signusing a switch statement:- If
signis '-' (indicating a negative exponent), it setsexponentNegativeto true and incrementsiby 1. - If
signis '+' (indicating a positive exponent), it incrementsiby 1.
- If
-
It enters a while loop that iterates while the index
iis less thanendIndex. -
Inside the loop, it retrieves the character at the index
ifrom thecharsarray and assigns it to the variablech. -
It checks the value of
chusing a switch statement:- If
chis a digit from '0' to '9', it converts the character to its corresponding integer value and adds it to theexponentvariable. It then incrementsiby 1. - If
chis not a digit, it throws anUnexpectedCharacterExceptionwith an appropriate error message indicating that an illegal character was encountered during parsing.
- If
-
After the loop, it checks if
exponentNegativeis true. If so, it negates the value ofexponent. -
It enters an if-else condition to calculate the power of 10 based on the value of
exponentand update theresultaccordingly:- If
exponentNegativeis false (indicating a positive exponent), it enters a while loop that continues whileexponentis greater than or equal topowersOf10.length. Inside the loop, it multipliesresultby 1e18f (a constant representing 10^18) and subtracts 18 fromexponent. Finally, it multipliesresultby the power of 10 stored in thepowersOf10array at the indexexponent. - If
exponentNegativeis true (indicating a negative exponent), it enters a while loop that continues while the negative value ofexponentis greater than or equal topowersOf10.length. Inside the loop, it dividesresultby 1e18f and adds 18 to the negativeexponent. Finally, it dividesresultby the power of 10 stored in thepowersOf10array at the index-exponent.
- If
-
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.

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.
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.
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) {
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:
- Initialize a boolean variable
negativetofalse. - Initialize an integer variable
ito thestartIndexparameter. - Initialize a double variable
resultto0. - Check if the character at index
iin thecharsarray is a negative sign ('-'). If it is, setnegativetotrueand incrementiby 1. - Enter a loop that iterates while
iis less thanendIndex. - Get the character at index
iin thecharsarray and assign it to the variablech. - Use a switch statement to check the value of
ch:- If
chis a digit ('0' to '9'), multiplyresultby 10 and add the numerical value ofch(obtained by subtracting the character '0') to it. Then, incrementiby 1. - If
chis a period ('.'), call theparseFractionPartmethod with the parametersi + 1,endIndex,chars, andresult. Assign the returned value toresult, and break the loop using thebreak loopstatement. - If
chis an 'E' or 'e', call theparseExponentmethod with the parametersi + 1,endIndex,chars, andresult. Assign the returned value toresult, and break the loop using thebreak loopstatement. - If
chis none of the above, throw anUnexpectedCharacterExceptionwith the appropriate message, character, and position.
- If
- If
negativeistrue, negateresult. - Return the value of
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;
}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:
- Initialize the fraction variable to 0.1.
- Start a while loop with the condition i < endIndex.
- Fetch the character at the current index from the character array and store it in the variable ch.
- 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.
- If ch is a digit (0-9), perform the following steps:
- After the while loop ends, return the final result.
Please let me know if you need more information.

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:
-
The method takes in four parameters:
i(the current index of thechararray),endIndex(the index of the last character in thechararray),chars(thechararray containing the number), andresult(the parsed value of the number so far). -
The method initializes a boolean variable
exponentNegativetofalseand an integer variableexponentto0. It also stores the value of thei-th character in thecharsarray in a variablesign. -
The method then enters a switch statement based on the value of
sign: a. Ifsignis'-', theexponentNegativevariable is set totrueand the value ofiis incremented. b. Ifsignis'+', the value ofiis incremented. -
The method then enters a while loop that continues as long as
iis less thanendIndex. -
Inside the loop, the method retrieves the character at the
i-th index of thecharsarray and enters a switch statement based on its value. a. If the character is a digit (0-9), the value ofexponentis updated by multiplying it by 10 and adding the difference between the character value and the character value of '0'. The value ofiis also incremented. b. If the character is not a digit, anUnexpectedCharacterExceptionis thrown with an appropriate error message. -
After the while loop, the method checks if
exponentNegativeistrue. If it is, the value ofexponentis negated. -
The method then enters an
ifstatement to handle the calculation of the power of 10. a. IfexponentNegativeisfalse, a while loop is initiated that runs as long asexponentis greater than or equal to the length ofpowersOf10(presumably an array storing pre-calculated powers of 10).- Inside the loop,
resultis multiplied by 1e22 (which is 10 raised to the power 22) andexponentis decreased by 22. b. Finally,resultis multiplied by the power of 10 corresponding toexponent.
- Inside the loop,
-
If
exponentNegativeistrue, a similar while loop is initiated but with negatedexponentand division instead of multiplication. -
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.

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.
- Java Open AI Client
- Using ChatGpt embeddings and hyde to improve search results
- Anthropics Claude Chatbot Gets Upgrade
- Elon Musks XAi new frontier for artificial intelligence
- Using Mockito to test JAI Java Open AI Client
- Fine tuning journey with Open AI API
- Using Open AI to create callback functions, the basis for plugins
- Using Java Open AI Client Async
- Fastest Java JSON Parser
- Java Open AI API Client on Github
- Medium: Introducing Java Open AI Client
- Medium: Using ChatGPT, Embeddings, and HyDE to Improve Search Results