字符比較函數(shù) cJSON_strcasecmp(const char *,const char *)
第一行處理兩個參數(shù)至少有一個為NULL的情況 如果一個是null 返回1 都是null 返回0
第二行逐個字符比較兩個字符串 完全一樣返回0
第三行處理s2字符串比s1字符串長的情況 返回多出來的第一個字符的ascii碼的相反數(shù)
字符串按照順序分析疏旨,將字符串分為符號位 整數(shù)位 小數(shù)位 科學(xué)計數(shù)法部分?
n=sign*n*pow(10.0,(scale+subscale*signsubscale)); /* number = +/- number.fraction * 10^+/- exponent */
static int pow2gt (int x) { --x; x|=x>>1; x|=x>>2; x|=x>>4; x|=x>>8; x|=x>>16; return x+1; }
位運算 返回一個比x大的最小的一個2的冪數(shù) 比如3 返回4 5 返回 8 117 返回32等消约,估計是用于返回要申請的最小的內(nèi)存大小
typedef struct {char *buffer; int length; int offset; } printbuffer;
printbuffer 一個字符串的封裝 封裝長度和當(dāng)前讀取到的offset
static char* ensure(printbuffer *p,int needed)
{
char *newbuffer;int newsize;
if (!p || !p->buffer) return 0;
needed+=p->offset;
if (needed<=p->length) return p->buffer+p->offset;
newsize=pow2gt(needed);
newbuffer=(char*)cJSON_malloc(newsize);
if (!newbuffer) {cJSON_free(p->buffer);p->length=0,p->buffer=0;return 0;}
if (newbuffer) memcpy(newbuffer,p->buffer,p->length);
cJSON_free(p->buffer);
p->length=newsize;
p->buffer=newbuffer;
return newbuffer+p->offset;
}
本段代碼確保新增加的needed的長度的字符串能被保存锥债,printbuffer實際上是一段存儲字符串的內(nèi)存塊扶歪,與自增長度的數(shù)組相似玄糟,ensure函數(shù)就是自增的操作瞳步,采取double內(nèi)存的操作
static int update(printbuffer *p)
{
char *str;
if (!p || !p->buffer) return 0;
str=p->buffer+p->offset;
return p->offset+strlen(str);
}
本函數(shù)更新printbuffer結(jié)構(gòu),新的字符串壓入printbuffer結(jié)構(gòu)體后站楚,調(diào)用本函數(shù),通過計算新壓入內(nèi)存的字符串的長度計算目前printbuffer結(jié)構(gòu)體的偏移量搏嗡,即已經(jīng)使用的內(nèi)存數(shù)量窿春。
static unsigned parse_hex4(const char *str)
{
unsigned h=0;
if (*str>='0' && *str<='9') h+=(*str)-'0'; else if (*str>='A' && *str<='F') h+=10+(*str)-'A'; else if (*str>='a' && *str<='f') h+=10+(*str)-'a'; else return 0;
h=h<<4;str++;
if (*str>='0' && *str<='9') h+=(*str)-'0'; else if (*str>='A' && *str<='F') h+=10+(*str)-'A'; else if (*str>='a' && *str<='f') h+=10+(*str)-'a'; else return 0;
h=h<<4;str++;
if (*str>='0' && *str<='9') h+=(*str)-'0'; else if (*str>='A' && *str<='F') h+=10+(*str)-'A'; else if (*str>='a' && *str<='f') h+=10+(*str)-'a'; else return 0;
h=h<<4;str++;
if (*str>='0' && *str<='9') h+=(*str)-'0'; else if (*str>='A' && *str<='F') h+=10+(*str)-'A'; else if (*str>='a' && *str<='f') h+=10+(*str)-'a'; else return 0;
return h;
}
四位十六進(jìn)制字符串轉(zhuǎn)成數(shù)字 “aaaa”=>1010101010101010=>43690
/* Parse the input text into an unescaped cstring, and populate item. */
static const unsigned char firstByteMark[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
static const char *parse_string(cJSON *item,const char *str,const char **ep)
{
const char *ptr=str+1,*end_ptr=str+1;char *ptr2;char *out;int len=0;unsigned uc,uc2;
if (*str!='\"') {*ep=str;return 0;} /* not a string! */
while (*end_ptr!='\"' && *end_ptr && ++len) if (*end_ptr++ == '\\') end_ptr++; /* Skip escaped quotes. */
out=(char*)cJSON_malloc(len+1); /* This is how long we need for the string, roughly. */
if (!out) return 0;
item->valuestring=out; /* assign here so out will be deleted during cJSON_Delete() later */
item->type=cJSON_String;
ptr=str+1;ptr2=out;
while (ptr < end_ptr)
{
if (*ptr!='\\') *ptr2++=*ptr++;
else
{
ptr++;
switch (*ptr)
{
case 'b': *ptr2++='\b'; break;
case 'f': *ptr2++='\f'; break;
case 'n': *ptr2++='\n'; break;
case 'r': *ptr2++='\r'; break;
case 't': *ptr2++='\t'; break;
case 'u': /* transcode utf16 to utf8. */
uc=parse_hex4(ptr+1);ptr+=4; /* get the unicode char. */
if (ptr >= end_ptr) {*ep=str;return 0;} /* invalid */
if ((uc>=0xDC00 && uc<=0xDFFF) || uc==0)? ? {*ep=str;return 0;} /* check for invalid.? */
if (uc>=0xD800 && uc<=0xDBFF) /* UTF16 surrogate pairs. */
{
if (ptr+6 > end_ptr)? ? {*ep=str;return 0;} /* invalid */
if (ptr[1]!='\\' || ptr[2]!='u')? ? {*ep=str;return 0;} /* missing second-half of surrogate.? ? */
uc2=parse_hex4(ptr+3);ptr+=6;
if (uc2<0xDC00 || uc2>0xDFFF)? ? ? {*ep=str;return 0;} /* invalid second-half of surrogate.? ? */
uc=0x10000 + (((uc&0x3FF)<<10) | (uc2&0x3FF));
}
len=4;if (uc<0x80) len=1;else if (uc<0x800) len=2;else if (uc<0x10000) len=3; ptr2+=len;
switch (len) {
case 4: *--ptr2 =((uc | 0x80) & 0xBF); uc >>= 6;
case 3: *--ptr2 =((uc | 0x80) & 0xBF); uc >>= 6;
case 2: *--ptr2 =((uc | 0x80) & 0xBF); uc >>= 6;
case 1: *--ptr2 =(uc | firstByteMark[len]);
}
ptr2+=len;
break;
default:? *ptr2++=*ptr; break;
}
ptr++;
}
}
*ptr2=0;
if (*ptr=='\"') ptr++;
return ptr;
}
處理字符串和unicode字符 utf8 only 不支持utf16等
待仔細(xì)品味