JSON grammar मान लिया जाये कि (लिंक वर्तमान में नीचे, Google cached version here) आप के लिए स्वीकार्य है, निम्नलिखित कम या ज्यादा प्रत्यक्ष आंतरिक रूप से विकसित कोड से JSON को पार्स करने आता है, अपने वाक्यविन्यास आरेख की एक शाब्दिक कार्यान्वयन किया जा रहा है:
/*
defined functions for handling the input:
nextChar() - peeks at the next character of input
getAndRemoveCharacter() - returns the next character of input and
dequeues it
This code also assumes you have BOOL, YES and NO defined; I've left this in
for clarity
*/
double getNumber()
{
// determine whether the number is negative - it'll start with a '-' if so
BOOL negative = NO;
if(nextChar() == '-')
{
negative = YES;
getAndRemoveCharacter();
}
// seed the output number to 0
double number = 0.0;
// if the next character isn't a '0' then this is the number proper, so
// just pull off the digits and assemble the number; otherwise this number
// is either 0 itself (in which case the initial seed is correct) or a
// decimal starting in 0
if(nextChar() != '0')
{
while(nextChar() >= '0' && nextChar() <= '9')
{
number *= 10.0;
number += getAndRemoveCharacter() - '0';
}
}
else
getAndRemoveCharacter();
// if this is a decimal then jump on to the decimal part and deserialise
// digits, much as above
if(nextChar() == '.')
{
getAndRemoveCharacter();
double decimalMultiplier = 1.0;
while(nextChar() >= '0' && nextChar() <= '9')
{
decimalMultiplier /= 10.0;
number += (double)(getAndRemoveCharacter() - '0') * decimalMultiplier;
}
}
// if this number has an exponent then deal with that
if(nextChar() == 'e' || nextChar() == 'E')
{
getAndRemoveCharacter();
double exponent = 0.0;
BOOL exponentPositive = YES;
// JSON allows positive exponents to start with + (unlike
// the mantissa) and requires negative exponents to start with -
if(nextChar() == '+')
{
getAndRemoveCharacter();
}
else
if(nextChar() == '-')
{
exponentPositive = NO;
getAndRemoveCharacter();
}
// read out digits and assemble exponent
while(nextChar() >= '0' && nextChar() <= '9')
{
exponent *= 10.0;
exponent += getAndRemoveCharacter() - '0';
}
// apply exponent
number *= pow(10.0, exponentPositive ? exponent : -exponent);
}
// negate if necessary and return
return negative ? -number : number;
}
सामान्य ASCII रेंज में एएससीआईआई अक्षरों को रखने वाला कोई भी चरित्र प्रकार काम करेगा, इसलिए इसे एएससीआईआईआई और वेरिएंट्स और यूनिकोड पर समान रूप से काम करना चाहिए। मुझे लगता है कि आप शायद उन सभी कॉल करने के बजाय सीधे एक तर्क के रूप में एक स्ट्रिंग लेना चाहते हैं; वे मूल में हैं क्योंकि इनपुट स्ट्रीम दूर से आ रही है, इसलिए वे ब्लॉक कर सकते हैं।
एकमात्र math.h फ़ंक्शन 'पाउ' में उपयोग किया जाता है, बाकी सब कुछ केवल प्राचीन परिचालन है।
आप "strtod" के स्रोत की खोज कर सकते हैं और उन्हें कॉपी कर सकते हैं। लाइसेंस से अवगत रहें! (स्ट्रेटोड वह फ़ंक्शन है जो तारों में तारों को युगल में परिवर्तित करता है)। मैंने एक संस्करण देखा, और यह लिखना बहुत मुश्किल प्रतीत नहीं होता है। – xanatos
@xanatos, मैंने प्रश्न पूछने से पहले glibc स्रोतों को देखा; हालांकि, एल्गोरिदम में एकाधिक परिशुद्धता अंकगणित शामिल है (और कागज को पढ़ने के बिना समझना मुश्किल है --- यदि कोई --- --- यह आधारित है)। – avakar