jerry_utf8.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2019-12-19 bernard the first version
  9. */
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <rtthread.h>
  14. #include "jerry_utf8.h"
  15. #ifdef PKG_USING_GUIENGINE
  16. #include <rtgui/gb2312.h>
  17. #endif
  18. bool jerry_str_is_utf8(const void* str, int size)
  19. {
  20. bool ret = true;
  21. unsigned char* start = (unsigned char*)str;
  22. unsigned char* end = (unsigned char*)str + size;
  23. while (start < end)
  24. {
  25. if (*start < 0x80) // (10000000): ASCII < 0x80
  26. {
  27. start++;
  28. }
  29. else if (*start < (0xC0)) // (11000000): 0x80 <= invalid ASCII < 0xC0
  30. {
  31. ret = false;
  32. break;
  33. }
  34. else if (*start < (0xE0)) // (11100000): two bytes UTF8
  35. {
  36. if (start >= end - 1)
  37. {
  38. break;
  39. }
  40. if ((start[1] & (0xC0)) != 0x80)
  41. {
  42. ret = false;
  43. break;
  44. }
  45. start += 2;
  46. }
  47. else if (*start < (0xF0)) // (11110000): three bytes UTF-8
  48. {
  49. if (start >= end - 2)
  50. {
  51. break;
  52. }
  53. if ((start[1] & (0xC0)) != 0x80 || (start[2] & (0xC0)) != 0x80)
  54. {
  55. ret = false;
  56. break;
  57. }
  58. start += 3;
  59. }
  60. else
  61. {
  62. ret = false;
  63. break;
  64. }
  65. }
  66. return ret;
  67. }
  68. int jerry_str2utf8(char *str, int len, char **utf8)
  69. {
  70. #ifdef PKG_USING_GUIENGINE
  71. Gb2312ToUtf8(str, len, utf8);
  72. #endif
  73. return 0;
  74. }