MCPcopy Create free account
hub / github.com/SaschaWillems/HowToVulkan / tryParseDouble

Function tryParseDouble

source/external/tinyobj/tiny_obj_loader.h:908–1039  ·  view source on GitHub ↗

Tries to parse a floating point number located at s. s_end should be a location in the string where reading should absolutely stop. For example at the end of the string, to prevent buffer overflows. Parses the following EBNF grammar: sign = "+" | "-" ; END = ? anything not in digit ? digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ; integer = [sign] , digit , {digit} ;

Source from the content-addressed store, hash-verified

906// - parse failure.
907//
908static bool tryParseDouble(const char *s, const char *s_end, double *result) {
909 if (s >= s_end) {
910 return false;
911 }
912
913 double mantissa = 0.0;
914 // This exponent is base 2 rather than 10.
915 // However the exponent we parse is supposed to be one of ten,
916 // thus we must take care to convert the exponent/and or the
917 // mantissa to a * 2^E, where a is the mantissa and E is the
918 // exponent.
919 // To get the final double we will use ldexp, it requires the
920 // exponent to be in base 2.
921 int exponent = 0;
922
923 // NOTE: THESE MUST BE DECLARED HERE SINCE WE ARE NOT ALLOWED
924 // TO JUMP OVER DEFINITIONS.
925 char sign = '+';
926 char exp_sign = '+';
927 char const *curr = s;
928
929 // How many characters were read in a loop.
930 int read = 0;
931 // Tells whether a loop terminated due to reaching s_end.
932 bool end_not_reached = false;
933 bool leading_decimal_dots = false;
934
935 /*
936 BEGIN PARSING.
937 */
938
939 // Find out what sign we've got.
940 if (*curr == '+' || *curr == '-') {
941 sign = *curr;
942 curr++;
943 if ((curr != s_end) && (*curr == '.')) {
944 // accept. Somethig like `.7e+2`, `-.5234`
945 leading_decimal_dots = true;
946 }
947 } else if (IS_DIGIT(*curr)) { /* Pass through. */
948 } else if (*curr == '.') {
949 // accept. Somethig like `.7e+2`, `-.5234`
950 leading_decimal_dots = true;
951 } else {
952 goto fail;
953 }
954
955 // Read the integer part.
956 end_not_reached = (curr != s_end);
957 if (!leading_decimal_dots) {
958 while (end_not_reached && IS_DIGIT(*curr)) {
959 mantissa *= 10;
960 mantissa += static_cast<int>(*curr - 0x30);
961 curr++;
962 read++;
963 end_not_reached = (curr != s_end);
964 }
965

Callers 1

parseRealFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected