You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.9 KiB

  1. // Copyright (c) 2022 Zhendong Peng (pzd17@tsinghua.org.cn)
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "wetext_string.h"
  15. #include "wetext_log.h"
  16. namespace wetext {
  17. const char* WHITESPACE = " \n\r\t\f\v";
  18. int UTF8CharLength(char ch) {
  19. int num_bytes = 1;
  20. CHECK_LE((ch & 0xF8), 0xF0);
  21. if ((ch & 0x80) == 0x00) {
  22. // The first 128 characters (US-ASCII) in UTF-8 format only need one byte.
  23. num_bytes = 1;
  24. } else if ((ch & 0xE0) == 0xC0) {
  25. // The next 1,920 characters need two bytes to encode,
  26. // which covers the remainder of almost all Latin-script alphabets.
  27. num_bytes = 2;
  28. } else if ((ch & 0xF0) == 0xE0) {
  29. // Three bytes are needed for characters in the rest of
  30. // the Basic Multilingual Plane, which contains virtually all characters
  31. // in common use, including most Chinese, Japanese and Korean characters.
  32. num_bytes = 3;
  33. } else if ((ch & 0xF8) == 0xF0) {
  34. // Four bytes are needed for characters in the other planes of Unicode,
  35. // which include less common CJK characters, various historic scripts,
  36. // mathematical symbols, and emoji (pictographic symbols).
  37. num_bytes = 4;
  38. }
  39. return num_bytes;
  40. }
  41. int UTF8StringLength(const std::string& str) {
  42. int len = 0;
  43. int num_bytes = 1;
  44. for (size_t i = 0; i < str.length(); i += num_bytes) {
  45. num_bytes = UTF8CharLength(str[i]);
  46. ++len;
  47. }
  48. return len;
  49. }
  50. void SplitUTF8StringToChars(const std::string& str,
  51. std::vector<std::string>* chars) {
  52. chars->clear();
  53. int num_bytes = 1;
  54. for (size_t i = 0; i < str.length(); i += num_bytes) {
  55. num_bytes = UTF8CharLength(str[i]);
  56. chars->push_back(str.substr(i, num_bytes));
  57. }
  58. }
  59. std::string Ltrim(const std::string& str) {
  60. size_t start = str.find_first_not_of(WHITESPACE);
  61. return (start == std::string::npos) ? "" : str.substr(start);
  62. }
  63. std::string Rtrim(const std::string& str) {
  64. size_t end = str.find_last_not_of(WHITESPACE);
  65. return end == std::string::npos ? "" : str.substr(0, end + 1);
  66. }
  67. std::string Trim(const std::string& str) { return Rtrim(Ltrim(str)); }
  68. void Split(const std::string& str, const std::string& delim,
  69. std::vector<std::string>* output) {
  70. std::string s = str;
  71. size_t pos = 0;
  72. while ((pos = s.find(delim)) != std::string::npos) {
  73. output->emplace_back(s.substr(0, pos));
  74. s.erase(0, pos + delim.length());
  75. }
  76. output->emplace_back(s);
  77. }
  78. } // namespace wetext