//options应该是5.3版本之后才支持的,由以下常量组成的二进制掩码: JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT, JSON_UNESCAPED_UNICODE.虽然我没用过。。。
static void json_escape_string(smart_str *buf, char *s, int len, int options TSRMLS_DC) /* {{{ */
{
int pos = 0;
unsigned short us;
unsigned short *utf16;
if (len == 0) { //如果长度为0,则直接返回 双引号 ""
smart_str_appendl(buf, """" , 2);
return ;
}
if (options & PHP_JSON_NUMERIC_CHECK) { //检测是否为0-9的数字,如果是数字,那么就会直接把数据作为long或double类型返回。
double d;
int type;
long p;
if ((type = is_numeric_string(s, len, &p, &d, 0)) != 0) {
if (type == IS_LONG) {
smart_str_append_long(buf, p);
} else if (type == IS_DOUBLE) {
if (!zend_isinf(d) && !zend_isnan(d)) {
char *tmp;
int l = spprintf(&tmp, 0, "%.*k" , ( int ) EG(precision), d);
smart_str_appendl(buf, tmp, l);
efree(tmp);
} else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "double %.9g does not conform to the JSON spec, encoded as 0" , d);
smart_str_appendc(buf, '0' );
}
}
return ;
}
}
utf16 = (unsigned short *) safe_emalloc(len, sizeof (unsigned short ), 0);
len = utf8_to_utf16(utf16, s, len); //这里会对你输入的值一次处理转成对应的Dec码,比如1是49,a是97这样的,保存到utf16中。
if (len //如果len小于0 说明出错。如果用json_encode处理GBK的编码,就会在这里挂掉。
if (utf16) {
efree(utf16);
}
if (len
JSON_G(error_code) = PHP_JSON_ERROR_UTF8;
if (!PG(display_errors)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid UTF-8 sequence in argument" );
}
smart_str_appendl(buf, "null" , 4);
} else {
smart_str_appendl(buf, """" , 2);
}
return ;
}
smart_str_appendc(buf, '"' ); //输入 "
//下面这一段代码就是将一些特殊字符转义如 双引号,反斜线等等
while (pos
{
us = utf16[pos++];
switch (us)
{
case '"' :
if (options & PHP_JSON_HEX_QUOT) {
smart_str_appendl(buf, "\u0022" , 6);
} else {
smart_str_appendl(buf, "\"" , 2);
}
break ;
case '\' :
smart_str_appendl(buf, "\\" , 2);
break ;
case '/' :
smart_str_appendl(buf, "\/" , 2);
break ;
case '' :
smart_str_appendl(buf, "\b" , 2);
break ;
case '' :
smart_str_appendl(buf, "\f" , 2);
break ;
case '
' :
smart_str_appendl(buf, "\n" , 2);
break ;
case '
' :
smart_str_appendl(buf, "\r" , 2);
break ;
case ' ' :
smart_str_appendl(buf, "\t" , 2);
break ;
case ':
if (options & PHP_JSON_HEX_TAG) {
smart_str_appendl(buf, "\u003C" , 6);
} else {
smart_str_appendc(buf, ');
}
break ;
case '>' :
if (options & PHP_JSON_HEX_TAG) {
smart_str_appendl(buf, "\u003E" , 6);
} else {
smart_str_appendc(buf, '>' );
}
break ;
case '&' :
if (options & PHP_JSON_HEX_AMP) {
smart_str_appendl(buf, "\u0026" , 6);
} else {
smart_str_appendc(buf, '&' );
}
break ;
case ''' :
if (options & PHP_JSON_HEX_APOS) {
smart_str_appendl(buf, "\u0027" , 6);
} else {
smart_str_appendc(buf, ''' );
}
break ;
default : //一直到这里,没有特殊字符就会把值append到buf中
if (us >= ' ' && (us & 127) == us) {
smart_str_appendc(buf, (unsigned char ) us);
} else {
smart_str_appendl(buf, "\u" , 2);
us = REVERSE16(us);
smart_str_appendc(buf, digits[us & ((1
us >>= 4;
smart_str_appendc(buf, digits[us & ((1
us >>= 4;
smart_str_appendc(buf, digits[us & ((1
us >>= 4;
smart_str_appendc(buf, digits[us & ((1
}
break ;
}
}
smart_str_appendc(buf, '"' ); //结束 双引号。
efree(utf16);
}
|