std::string valueToQuotedString( const char *value ) { // Not sure how to handle unicode... if( strpbrk(value, "\"\\\b\f\n\r\t") == NULL) return std::string("\"") + value + "\""; // We have to walk value and escape any special characters. // Appending to std::string is not efficient, but this should be rare. // (Note: forward slashes are *not* rare, but I am not escaping them.) unsigned maxsize = strlen(value)*2 + 3; // allescaped+quotes+NULL std::string result; result.reserve(maxsize); // to avoid lots of mallocs result += "\""; for (const char* c=value; *c != 0; ++c) { if( IsGB(*c) )//支持中文,例如"" { result += *c; c++; if( 0 == (*c) ) break; result += *c; continue; } switch(*c) { case '\"': result += "\\\""; break; case '\\': result += "\\\\"; break; case '\b': result += "\\b"; break; case '\f': result += "\\f"; break; case '\n': result += "\\n"; break; case '\r': result += "\\r"; break; case '\t': result += "\\t"; break; case '/': // Even though \/ is considered a legal escape in JSON, a bare // slash is also legal, so I see no reason to escape it. // (I hope I am not misunderstanding something.) default: result += *c; } } result += "\""; return result; }
HZ get_hz() { unsigned char char1, char2; HZ hz; if (1 != fscanf_s(stdin, "%c", &char1, sizeof(char))) return END; if (char1 < 0xa0) return TERMINAL; if (1 != fscanf_s(stdin, "%c", &char2, sizeof(char))) return END; hz = char2 * 256 + char1; if (IsGB(hz)) return hz; return TERMINAL; }