Bernard Xiong 6 лет назад
Родитель
Сommit
91179b54c7
4 измененных файлов с 120 добавлено и 62 удалено
  1. 3 2
      rtthread-port/jerry_buffer.c
  2. 86 0
      rtthread-port/jerry_utf8.c
  3. 28 0
      rtthread-port/jerry_utf8.h
  4. 3 60
      rtthread-port/jerry_util.c

+ 3 - 2
rtthread-port/jerry_buffer.c

@@ -13,6 +13,7 @@
 #include <jerry_util.h>
 
 #include "jerry_buffer.h"
+#include "jerry_utf8.h"
 
 #define strequal(a, b) !strcmp(a, b)
 
@@ -321,7 +322,7 @@ DECLARE_HANDLER(toString)
 
         str += start;
         rt_free(enc);
-        if (is_utf8_string(str, end - start))
+        if (jerry_str_is_utf8(str, end - start))
             return jerry_create_string_sz_from_utf8(str, end - start);
         else
             return jerry_create_undefined();
@@ -345,7 +346,7 @@ DECLARE_HANDLER(toString)
         }
 
         jerry_value_t jstr;
-        if (is_utf8_string(str, index - start))
+        if (jerry_str_is_utf8(str, index - start))
             jstr = jerry_create_string_sz_from_utf8((jerry_char_t *)str, index - start);
         else
             jstr = jerry_create_undefined();

+ 86 - 0
rtthread-port/jerry_utf8.c

@@ -0,0 +1,86 @@
+/*
+ * Copyright (c) 2006-2018, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2019-12-19     bernard      the first version
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include <rtthread.h>
+#include "jerry_utf8.h"
+
+#ifdef PKG_USING_GUIENGINE
+#include <rtgui/gb2312.h>
+#endif
+
+bool jerry_str_is_utf8(const void* str, int size)
+{
+    bool ret = true;
+    unsigned char* start = (unsigned char*)str;
+    unsigned char* end = (unsigned char*)str + size;
+
+    while (start < end)
+    {
+        if (*start < 0x80)              // (10000000): ASCII < 0x80
+        {
+            start++;
+        }
+        else if (*start < (0xC0))       // (11000000): 0x80 <= invalid ASCII < 0xC0
+        {
+            ret = false;
+            break;
+        }
+        else if (*start < (0xE0))       // (11100000): two bytes UTF8
+        {
+            if (start >= end - 1)
+            {
+                break;
+            }
+
+            if ((start[1] & (0xC0)) != 0x80)
+            {
+                ret = false;
+                break;
+            }
+
+            start += 2;
+        }
+        else if (*start < (0xF0))       // (11110000): three bytes UTF-8
+        {
+            if (start >= end - 2)
+            {
+                break;
+            }
+
+            if ((start[1] & (0xC0)) != 0x80 || (start[2] & (0xC0)) != 0x80)
+            {
+                ret = false;
+                break;
+            }
+
+            start += 3;
+        }
+        else
+        {
+            ret = false;
+            break;
+        }
+    }
+
+    return ret;
+}
+
+int jerry_str2utf8(char *str, int len, char **utf8)
+{
+#ifdef PKG_USING_GUIENGINE
+    Gb2312ToUtf8(str, len, utf8);
+#endif
+
+    return 0;
+}

+ 28 - 0
rtthread-port/jerry_utf8.h

@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2006-2018, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2019-12-19     bernard      the first version
+ */
+
+#ifndef JERRY_UTF8_H__
+#define JERRY_UTF8_H__
+
+#include <stdbool.h>
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+bool jerry_str_is_utf8(const void* str, int size);
+int  jerry_str2utf8(char *str, int len, char **utf8);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif

+ 3 - 60
rtthread-port/jerry_util.c

@@ -8,7 +8,7 @@
 #include <jerry_event.h>
 #include <jerry_message.h>
 
-// #include <rtgui/gb2312.h>
+#include "jerry_utf8.h"
 
 extern int js_console_init(void);
 extern int js_module_init(void);
@@ -20,63 +20,6 @@ static void _js_value_dump(jerry_value_t value);
 static rt_mutex_t _util_lock = NULL;
 static js_util_user _user_init = NULL, _user_cleanup = NULL;
 
-bool is_utf8_string(const void* str, int size)
-{
-    bool ret = true;
-    unsigned char* start = (unsigned char*)str;
-    unsigned char* end = (unsigned char*)str + size;
-
-    while (start < end)
-    {
-        if (*start < 0x80) 				// (10000000): 值小于0x80的为ASCII字符
-        {
-            start++;
-        }
-        else if (*start < (0xC0)) 		// (11000000): 值介于0x80与0xC0之间的为无效UTF-8字符
-        {
-            ret = false;
-            break;
-        }
-        else if (*start < (0xE0)) 		// (11100000): 此范围内为2字节UTF-8字符
-        {
-            if (start >= end - 1)
-            {
-                break;
-            }
-
-            if ((start[1] & (0xC0)) != 0x80)
-            {
-                ret = false;
-                break;
-            }
-
-            start += 2;
-        }
-        else if (*start < (0xF0)) 		// (11110000): 此范围内为3字节UTF-8字符
-        {
-            if (start >= end - 2)
-            {
-                break;
-            }
-
-            if ((start[1] & (0xC0)) != 0x80 || (start[2] & (0xC0)) != 0x80)
-            {
-                ret = false;
-                break;
-            }
-
-            start += 3;
-        }
-        else
-        {
-            ret = false;
-            break;
-        }
-    }
-
-    return ret;
-}
-
 void js_set_property(const jerry_value_t obj, const char *name,
                      const jerry_value_t prop)
 {
@@ -132,7 +75,7 @@ jerry_value_t js_string_to_value(const char *value)
 {
     jerry_value_t str = 0;
 
-    if (is_utf8_string(value, strlen(value)))
+    if (jerry_str_is_utf8(value, strlen(value)))
     {
         str = jerry_create_string((const jerry_char_t *)value);
     }
@@ -140,7 +83,7 @@ jerry_value_t js_string_to_value(const char *value)
     {
         char *utf8 = NULL;
 
-        Gb2312ToUtf8((char *)value, strlen(value), &utf8);
+        jerry_str2utf8((char *)value, strlen(value), &utf8);
         str = jerry_create_string((const jerry_char_t *)utf8);
 
         rt_free(utf8);