# [PATCH] JSON_parser

- Thread: https://externals.io/message/29731
- Original archive: https://news-web.php.net/php.internals/29731
- Started by Martin Majlis on 2007-05-24T23:50:17+00:00
- Messages: 4
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #29731 — Martin Majlis — 2007-05-24T23:50:17+00:00

URL: https://externals.io/message/29731#29731

Just small refactoring. Replacing self-made function with functions
from standard headers.

Index: JSON_parser.c
===================================================================
RCS file: /repository/php-src/ext/json/JSON_parser.c,v
retrieving revision 1.1.2.8
diff -u -u -r1.1.2.8 JSON_parser.c
--- JSON_parser.c	24 May 2007 22:37:59 -0000	1.1.2.8
+++ JSON_parser.c	24 May 2007 23:41:11 -0000
@@ -29,6 +29,8 @@

 #include "JSON_parser.h"
 #include <stdio.h>
+#include <math.h>
+#include <ctype.h>

 #define true  1
 #define false 0
@@ -259,18 +261,10 @@

 static int dehexchar(char c)
 {
-    if (c >= '0' && c <= '9')
-    {
-        return c - '0';
-    }
-    else if (c >= 'A' && c <= 'F')
-    {
-        return c - ('A' - 10);
-    }
-    else if (c >= 'a' && c <= 'f')
-    {
-        return c - ('a' - 10);
-    }
+    if (isxdigit(c))
+    {
+        return strtol(&c, NULL, 16);
+    }
     else
     {
         return -1;

---

## #29732 — Ilia Alshanetsky — 2007-05-25T00:02:33+00:00

URL: https://externals.io/message/29731#29732 · In reply to: #29731

I think the current code is better and not to mention faster. Shorter  
code in this case does not mean better code.




> Just small refactoring. Replacing self-made function with functions
> from standard headers.
>
> Index: JSON_parser.c
> ===================================================================
> RCS file: /repository/php-src/ext/json/JSON_parser.c,v
> retrieving revision 1.1.2.8
> diff -u -u -r1.1.2.8 JSON_parser.c
> --- JSON_parser.c	24 May 2007 22:37:59 -0000	1.1.2.8
> +++ JSON_parser.c	24 May 2007 23:41:11 -0000
> @@ -29,6 +29,8 @@
>
> #include "JSON_parser.h"
> #include <stdio.h>
> +#include <math.h>
> +#include <ctype.h>
>
> #define true  1
> #define false 0
> @@ -259,18 +261,10 @@
>
> static int dehexchar(char c)
> {
> -    if (c >= '0' && c <= '9')
> -    {
> -        return c - '0';
> -    }
> -    else if (c >= 'A' && c <= 'F')
> -    {
> -        return c - ('A' - 10);
> -    }
> -    else if (c >= 'a' && c <= 'f')
> -    {
> -        return c - ('a' - 10);
> -    }
> +    if (isxdigit(c))
> +    {
> +        return strtol(&c, NULL, 16);
> +    }
>     else
>     {
>         return -1;
>
> -- 
> 
> 
>

Ilia Alshanetsky

---

## #29733 — Rasmus Lerdorf — 2007-05-25T00:03:10+00:00

URL: https://externals.io/message/29731#29733 · In reply to: #29731

What prompted this?  I'm not keen on changing the parser unless there is
some benefit.  Speed, portability, fixing a reported issue, etc.  The
one risk I see here is that isxdigit() is a LOCALE-aware function, so
first, it is likely slower than what it replaces here, and second, a
messed up LOCALE could potentially cause it to misbehave.

-Rasmus

Martin Majlis wrote:
> Just small refactoring. Replacing self-made function with functions
> from standard headers.
> 
> Index: JSON_parser.c
> ===================================================================
> RCS file: /repository/php-src/ext/json/JSON_parser.c,v
> retrieving revision 1.1.2.8
> diff -u -u -r1.1.2.8 JSON_parser.c
> --- JSON_parser.c    24 May 2007 22:37:59 -0000    1.1.2.8
> +++ JSON_parser.c    24 May 2007 23:41:11 -0000
> @@ -29,6 +29,8 @@
> 
> #include "JSON_parser.h"
> #include <stdio.h>
> +#include <math.h>
> +#include <ctype.h>
> 
> #define true  1
> #define false 0
> @@ -259,18 +261,10 @@
> 
> static int dehexchar(char c)
> {
> -    if (c >= '0' && c <= '9')
> -    {
> -        return c - '0';
> -    }
> -    else if (c >= 'A' && c <= 'F')
> -    {
> -        return c - ('A' - 10);
> -    }
> -    else if (c >= 'a' && c <= 'f')
> -    {
> -        return c - ('a' - 10);
> -    }
> +    if (isxdigit(c))
> +    {
> +        return strtol(&c, NULL, 16);
> +    }
>     else
>     {
>         return -1;

---

## #29743 — Martin Majlis — 2007-05-25T08:41:05+00:00

URL: https://externals.io/message/29731#29743 · In reply to: #29731

I make some small test.
http://martin.m-core.net/misc/php/dehex.c - source code

cc dehex.c; for i in `seq 1 1000`; do ./a.out >> dehex.log; done
http://martin.m-core.net/misc/php/dehex.log - results

I ignore values greater than 4e9. And here is summary:
http://martin.m-core.net/misc/php/dehex.php

Source of PHP script: http://martin.m-core.net/misc/php/dehex.phps

So, m1 is very slow, but other ones are faster.

Martin Majlis


> Just small refactoring. Replacing self-made function with functions
> from standard headers.
>
> Index: JSON_parser.c
> ===================================================================
> RCS file: /repository/php-src/ext/json/JSON_parser.c,v
> retrieving revision 1.1.2.8
> diff -u -u -r1.1.2.8 JSON_parser.c
> --- JSON_parser.c       24 May 2007 22:37:59 -0000      1.1.2.8
> +++ JSON_parser.c       24 May 2007 23:41:11 -0000
> @@ -29,6 +29,8 @@
>
>  #include "JSON_parser.h"
>  #include <stdio.h>
> +#include <math.h>
> +#include <ctype.h>
>
>  #define true  1
>  #define false 0
> @@ -259,18 +261,10 @@
>
>  static int dehexchar(char c)
>  {
> -    if (c >= '0' && c <= '9')
> -    {
> -        return c - '0';
> -    }
> -    else if (c >= 'A' && c <= 'F')
> -    {
> -        return c - ('A' - 10);
> -    }
> -    else if (c >= 'a' && c <= 'f')
> -    {
> -        return c - ('a' - 10);
> -    }
> +    if (isxdigit(c))
> +    {
> +        return strtol(&c, NULL, 16);
> +    }
>      else
>      {
>          return -1;
