fork download
  1. #include <stdio.h>
  2. #include <limits.h>
  3.  
  4. typedef struct jn_str_st
  5. {
  6. char* s;
  7. int len;
  8. }
  9. jn_str_t;
  10.  
  11. int jn_scan_json(char* in_buf, jn_str_t* out_toks, int* out_len)
  12. {
  13. int eqcls[256] = {
  14. 0x8, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  15. 0x0, 0x2, 0x2, 0x0, 0x0, 0x2, 0x0, 0x0,
  16. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  17. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  18. 0x2, 0x0, 0x33, 0x0, 0x0, 0x0, 0x0, 0x0,
  19. 0x0, 0x0, 0x0, 0x1000, 0x1, 0x1104, 0x400, 0x20,
  20. 0x2e84, 0x2f84, 0x2f84, 0x2f84, 0x2f84, 0x2f84, 0x2f84, 0x2f84,
  21. 0x2f84, 0x2f84, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0,
  22. 0x0, 0x80, 0x80, 0x80, 0x80, 0x880, 0x80, 0x0,
  23. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  24. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  25. 0x0, 0x0, 0x0, 0x1, 0x38, 0x1, 0x0, 0x0,
  26. 0x0, 0x20080, 0xa0, 0x80, 0x80, 0x110880, 0xa6, 0x0,
  27. 0x0, 0x0, 0x0, 0x0, 0x440000, 0x0, 0x27, 0x0,
  28. 0x0, 0x0, 0x4020, 0x80000, 0x25, 0x208040, 0x0, 0x0,
  29. 0x0, 0x0, 0x0, 0x1, 0x0, 0x1, 0x0, 0x0,
  30. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  31. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  32. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  33. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  34. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  35. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  36. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  37. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  38. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  39. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  40. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  41. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  42. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  43. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  44. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
  45. 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0
  46. };
  47.  
  48. int tbl[] = {
  49. 0x200, 0xa00, 0x7000d01, 0x7000c01, 0x3030e09, 0x3080a18, 0x10e0e36, 0x1110e3c,
  50. 0x1150e44, 0x3031609, 0x1200, 0x7001501, 0x305160d, 0x1a00, 0x3031e09, 0x1071e10,
  51. 0x2200, 0x1072612, 0x2a00, 0x1072e14, 0x3200, 0x1073616, 0x3a00, 0x3033e09,
  52. 0x4200, 0x308461c, 0x3084620, 0x3084624, 0x4a00, 0x4a00, 0x3084e20, 0x3084e24,
  53. 0x30a5228, 0x5200, 0x5200, 0x5200, 0x30a5a28, 0x5a00, 0x3085e24, 0x3085e24,
  54. 0x7006101, 0x30a662c, 0x30c6630, 0x6200, 0x7006901, 0x6a00, 0x30c6e30, 0x30a6e2c,
  55. 0x7200, 0x30c7633, 0x30c7633, 0x7007901, 0x7a00, 0x30c7e33, 0x8200, 0x10f8638,
  56. 0x8a00, 0x1108e3a, 0x9200, 0x7009501, 0x9a00, 0x1129e3e, 0xa200, 0x113a640,
  57. 0xaa00, 0x114ae42, 0xb200, 0x700b501, 0xba00, 0x116be46, 0xc200, 0x116c648,
  58. 0xca00, 0x700cd01 };
  59.  
  60. char* p = in_buf;
  61. char* s = in_buf;
  62. jn_str_t* t = out_toks;
  63. int state = 0x7000201;
  64. unsigned int cls = 0;
  65.  
  66. do
  67. {
  68. p += (state>>10) & 0x1;
  69. t->s = s;
  70. t->len = p - s;
  71. t += (state>>8) & 0x1;
  72. s += (p - s) & (unsigned)(UINT_MAX + ((state>>9) & 0x1));
  73. cls = eqcls[(unsigned char)(*p)];
  74. cls = (cls>>((state & 0x00FF0000)>>16))&(state>>24);
  75. state = tbl[ (state & 0x000000FF) + cls ];
  76. }
  77. while( (state & 0x000000FF) > 0);
  78.  
  79. *out_len = t - out_toks;
  80. cls = ((state & 0xf800) >> 11); /* fail state idx */
  81.  
  82. if(cls < 1) /* fail state idx */
  83. {
  84. if(*p == 0)
  85. {
  86. return 1; /* SUCCESS */
  87. }
  88. }
  89.  
  90. return 0; /* fail */
  91. }
  92.  
  93.  
  94. int main(void)
  95. {
  96. jn_str_t buf[0xffff] = {0};
  97. int buf_len = 0;
  98.  
  99. char* succ_json = "{\"key1\": \"this is test json\", \"key2\" : [0, 1, 12, 0.123, 12.234, -293, -394.294E-23]}";
  100.  
  101. jn_scan_json(succ_json, buf, &buf_len);
  102.  
  103.  
  104. for(int i=0; i < buf_len; i++)
  105. {
  106. printf("%.*s\n", buf[i].len, buf[i].s);
  107. }
  108.  
  109.  
  110. return 0;
  111. }
  112.  
  113.  
Success #stdin #stdout 0s 4560KB
stdin
Standard input is empty
stdout
{
"key1"
:
"this is test json"
,
"key2"
:
[
0
,
1
,
12
,
0.123
,
12.234
,
-293
,
-394.294E-23
]
}