fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <limits.h>
  5.  
  6. typedef struct
  7. {
  8. char* s;
  9. int len;
  10. } token_t;
  11.  
  12. int scan_tok(char* in_txt, token_t* out_buf, int* out_tok_len)
  13. {
  14. unsigned eqcls[256] = {
  15. 0,1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,
  16. 1568535294,1568535282,1568535282,1568535294,1568535294,1568535282,1568535294,1568535294,
  17. 1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,
  18. 1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,
  19. 1568535282,1568535294,1568535126,1568535294,1568535294,1568535294,1568535294,1568535294,
  20. 1568535294,1568535294,1568535294,1566438142,1568535284,1566438140,1568516862,1568535166,
  21. 1563092730,1563125496,1563125496,1563125496,1563125496,1563125496,1563125496,1563125496,
  22. 1563125496,1563125496,1568535284,1568535294,1568535294,1568535294,1568535294,1568535294,
  23. 1568535294,1568535038,1568535038,1568535038,1568535038,1568001534,1568535038,1568535038,
  24. 1568535038,1568535038,1568535038,1568535038,1568535038,1568535038,1568535038,1568535038,
  25. 1568535038,1568535038,1568535038,1568535038,1568535038,1568535038,1568535038,1568535038,
  26. 1568535038,1568535038,1568535038,1568535284,1568535150,1568535284,1568535294,1568535294,
  27. 1568535294,1560146430,1568534910,1568535038,1568535038,762695166,1568534910,1568535038,
  28. 1568535038,1568535038,1568535038,1568535038,1266545150,1568535038,1568534910,1568535038,
  29. 1568535038,1568535038,1560146302,1434317310,1568534910,1526591934,1568535038,1568535038,
  30. 1568535038,1568535038,1568535038,1568535284,1568535294,1568535284,1568535294,1568535294,
  31. 1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,
  32. 1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,
  33. 1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,
  34. 1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,
  35. 1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,
  36. 1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,
  37. 1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,
  38. 1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,
  39. 1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,
  40. 1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,
  41. 1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,
  42. 1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,
  43. 1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,
  44. 1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,
  45. 1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,
  46. 1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,1568535294,1568535294
  47. };
  48. unsigned tbl[] = {
  49. 98304, 98304, 475265, 483457, 246281, 509213, 247458, 247718,
  50. 248763, 98304, 483457, 246541, 246281, 98304, 246281, 246801,
  51. 246281, 98304, 246804, 102400, 98304, 246807, 102400, 98304,
  52. 246810, 102400, 98304, 246281, 102400, 98304, 509213, 247978,
  53. 248497, 467073, 98304, 247978, 248497, 467073, 98304, 247458,
  54. 509213, 102400, 98304, 248237, 102400, 98304, 248237, 248497,
  55. 467073, 98304, 247989, 247989, 102400, 98304, 247992, 102400,
  56. 98304, 247992, 467073, 98304, 249022, 102400, 98304, 249281,
  57. 102400, 98304, 483457, 249541, 102400, 98304, 483457, 102400
  58. };
  59. char* p = in_txt;
  60. char* s = p;
  61. token_t* ptk = out_buf;
  62. int state = 491649;
  63. do
  64. {
  65. unsigned cls = eqcls[(unsigned char)(*p)];
  66. cls = (cls >> ((state >> 7) & 31)) & (state >> 16);
  67. state = tbl[ (state & 127) + cls ];
  68. p += (state >> 14) & 1;
  69. ptk->s = s;
  70. ptk->len = p-s;
  71. ptk += (state >> 13) & 1;
  72. s += (p-s) & (UINT_MAX + ((state >> 15) & 1));
  73. }while( (state & 127) > 0);
  74.  
  75. *out_tok_len = ptk - out_buf;
  76.  
  77. /* error flag */
  78. if ( (state>>12) & 1 )
  79. return 0;
  80.  
  81. return 1;
  82. }
  83.  
  84. int main(void)
  85. {
  86. char* test1 = "\n { \"this is \\\\ \\\"test json\" : -0.12345e-5, \"test key1\": [true, false, null, tulse\
  87. , 0,1,2,8,9, 13, 2345, 30004.0, 300004.2134003, \
  88. ]\n }";
  89. token_t buf[512];
  90. int buf_len = 0;
  91. int ret = 0;
  92.  
  93. memset(buf, 0x00, sizeof(buf));
  94.  
  95. ret = scan_tok(test1, buf, &buf_len);
  96.  
  97. if(ret == 0)
  98. {
  99. printf("token error");
  100. exit(1);
  101. }
  102.  
  103. for(int i=0; i < buf_len; i++)
  104. {
  105. printf("%.*s\n", buf[i].len, buf[i].s);
  106. }
  107.  
  108. return 0;
  109. }
Success #stdin #stdout 0s 5408KB
stdin
Standard input is empty
stdout
{
"this is \\ \"test json"
:
-0.12345e-5
,
"test key1"
:
[
true
,
false
,
null
,
tulse
,
0
,
1
,
2
,
8
,
9
,
13
,
2345
,
30004.0
,
300004.2134003
,
]
}