-
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
negative
tofalse
, an integer variablei
to the value ofstartIndex
, and a float variableresult
to0
. -
It checks if the character at index
i
in thechars
array is'-'. If it is, it sets
negativeto
trueand increments
i` by 1. -
It enters a loop that continues until
i
is less thanendIndex
. -
Inside the loop, it assigns the character at index
i
in thechars
array to a char variablech
. -
It checks the value of
ch
using a switch statement. Ifch
is a digit from 0 to 9, it performs the following steps:- Multiplies the
result
by 10 and adds the numeric value ofch
minus the character '0' (to convert from ASCII to the actual numeric value). - Increments
i
by 1. - Breaks out of the switch statement.
- Multiplies the
-
If
ch
is a '.', it calls a methodparseFractionPart
with parametersi + 1
,endIndex
,chars
, andresult
and assigns the return value toresult
. -
If
ch
is an 'e', it calls a methodparseExponent
with parametersi + 1
,endIndex
,chars
, andresult
and assigns the return value toresult
. -
If
ch
does not match any of the cases in the switch statement, it throws anUnexpectedCharacterException
with the message "Illegal character" and the values of "parsing float", the actual characterch
, and the indexi
. -
After the loop ends, it checks if
negative
istrue
. 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
fraction
as 0.1. -
It enters a
while
loop that iterates fromi
toendIndex
. -
Within the loop, the method retrieves the character at the current index (
i
) from thechars
array and assigns it to a variablech
. -
It uses a
switch
statement to perform different actions based on the characterch
. -
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 theresult
accordingly. It multiplies the difference betweench
and'0'
byfraction
and adds it to theresult
. It then dividesfraction
by 10 to handle the next digit and incrementsi
to move to the next character. -
If the character
ch
is'e'
, the method returns the result of another method calledparseExponent
(which is not defined in the given code snippet). -
If the character
ch
is neither a digit nor'e'
, the method throws anUnexpectedCharacterException
with an appropriate error message. -
After each iteration of the loop,
i
is 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
exponentNegative
to false, which will indicate if the exponent is negative. -
It initializes an integer variable
exponent
to store the parsed exponent value. -
It retrieves the character at the index
i
from thechars
array and assigns it to the variablesign
. -
It checks the value of
sign
using a switch statement:- If
sign
is '-' (indicating a negative exponent), it setsexponentNegative
to true and incrementsi
by 1. - If
sign
is '+' (indicating a positive exponent), it incrementsi
by 1.
- If
-
It enters a while loop that iterates while the index
i
is less thanendIndex
. -
Inside the loop, it retrieves the character at the index
i
from thechars
array and assigns it to the variablech
. -
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 theexponent
variable. It then incrementsi
by 1. - If
ch
is not a digit, it throws anUnexpectedCharacterException
with an appropriate error message indicating that an illegal character was encountered during parsing.
- If
-
After the loop, it checks if
exponentNegative
is 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
exponent
and update theresult
accordingly:- If
exponentNegative
is false (indicating a positive exponent), it enters a while loop that continues whileexponent
is greater than or equal topowersOf10.length
. Inside the loop, it multipliesresult
by 1e18f (a constant representing 10^18) and subtracts 18 fromexponent
. Finally, it multipliesresult
by the power of 10 stored in thepowersOf10
array at the indexexponent
. - If
exponentNegative
is true (indicating a negative exponent), it enters a while loop that continues while the negative value ofexponent
is greater than or equal topowersOf10.length
. Inside the loop, it dividesresult
by 1e18f and adds 18 to the negativeexponent
. Finally, it dividesresult
by the power of 10 stored in thepowersOf10
array 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
negative
tofalse
. - Initialize an integer variable
i
to thestartIndex
parameter. - Initialize a double variable
result
to0
. - Check if the character at index
i
in thechars
array is a negative sign ('-'). If it is, setnegative
totrue
and incrementi
by 1. - Enter a loop that iterates while
i
is less thanendIndex
. - Get the character at index
i
in thechars
array and assign it to the variablech
. - Use a switch statement to check the value of
ch
:- If
ch
is a digit ('0' to '9'), multiplyresult
by 10 and add the numerical value ofch
(obtained by subtracting the character '0') to it. Then, incrementi
by 1. - If
ch
is a period ('.'), call theparseFractionPart
method with the parametersi + 1
,endIndex
,chars
, andresult
. Assign the returned value toresult
, and break the loop using thebreak loop
statement. - If
ch
is an 'E' or 'e', call theparseExponent
method with the parametersi + 1
,endIndex
,chars
, andresult
. Assign the returned value toresult
, and break the loop using thebreak loop
statement. - If
ch
is none of the above, throw anUnexpectedCharacterException
with the appropriate message, character, and position.
- If
- If
negative
istrue
, 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 thechar
array),endIndex
(the index of the last character in thechar
array),chars
(thechar
array containing the number), andresult
(the parsed value of the number so far). -
The method initializes a boolean variable
exponentNegative
tofalse
and an integer variableexponent
to0
. It also stores the value of thei
-th character in thechars
array in a variablesign
. -
The method then enters a switch statement based on the value of
sign
: a. Ifsign
is'-'
, theexponentNegative
variable is set totrue
and the value ofi
is incremented. b. Ifsign
is'+'
, the value ofi
is incremented. -
The method then enters a while loop that continues as long as
i
is less thanendIndex
. -
Inside the loop, the method retrieves the character at the
i
-th index of thechars
array and enters a switch statement based on its value. a. If the character is a digit (0-9), the value ofexponent
is updated by multiplying it by 10 and adding the difference between the character value and the character value of '0'. The value ofi
is also incremented. b. If the character is not a digit, anUnexpectedCharacterException
is thrown with an appropriate error message. -
After the while loop, the method checks if
exponentNegative
istrue
. If it is, the value ofexponent
is negated. -
The method then enters an
if
statement to handle the calculation of the power of 10. a. IfexponentNegative
isfalse
, a while loop is initiated that runs as long asexponent
is greater than or equal to the length ofpowersOf10
(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) andexponent
is decreased by 22. b. Finally,result
is multiplied by the power of 10 corresponding toexponent
.
- Inside the loop,
-
If
exponentNegative
istrue
, a similar while loop is initiated but with negatedexponent
and 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