codepage.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. #pragma once
  2. #include "doccodes.h"
  3. #include <util/charset/recode_result.h>
  4. #include <util/charset/unidata.h> // all wchar32 functions
  5. #include <util/charset/utf8.h>
  6. #include <util/generic/string.h>
  7. #include <util/generic/ylimits.h>
  8. #include <util/generic/yexception.h>
  9. #include <util/system/yassert.h>
  10. #include <util/system/defaults.h>
  11. #include <cctype>
  12. struct CodePage;
  13. struct Recoder;
  14. struct Encoder;
  15. /*****************************************************************\
  16. * struct CodePage *
  17. \*****************************************************************/
  18. struct CodePage {
  19. ECharset CPEnum; // int MIBEnum;
  20. const char* Names[30]; // name[0] -- preferred mime-name
  21. wchar32 unicode[256];
  22. const char* DefaultChar; //[CCL_NUM]
  23. bool IsLower(unsigned char ch) const {
  24. return ::IsLower(unicode[ch]);
  25. }
  26. bool IsUpper(unsigned char ch) const {
  27. return ::IsUpper(unicode[ch]);
  28. }
  29. bool IsAlpha(unsigned char ch) const {
  30. return ::IsAlpha(unicode[ch]);
  31. }
  32. bool IsDigit(unsigned char ch) const {
  33. return ::IsDigit(unicode[ch]);
  34. }
  35. bool IsXdigit(unsigned char ch) const {
  36. return ::IsXdigit(unicode[ch]);
  37. }
  38. bool IsAlnum(unsigned char ch) const {
  39. return ::IsAlnum(unicode[ch]);
  40. }
  41. bool IsSpace(unsigned char ch) const {
  42. return ::IsSpace(unicode[ch]);
  43. }
  44. bool IsPunct(unsigned char ch) const {
  45. return ::IsPunct(unicode[ch]);
  46. }
  47. bool IsCntrl(unsigned char ch) const {
  48. return ::IsCntrl(unicode[ch]);
  49. }
  50. bool IsGraph(unsigned char ch) const {
  51. return ::IsGraph(unicode[ch]);
  52. }
  53. bool IsPrint(unsigned char ch) const {
  54. return ::IsPrint(unicode[ch]);
  55. }
  56. bool IsComposed(unsigned char ch) const {
  57. return ::IsComposed(unicode[ch]);
  58. }
  59. // return pointer to char after the last char
  60. char* ToLower(const char* begin, const char* end, char* to) const;
  61. char* ToLower(const char* begin, char* to) const;
  62. // return pointer to char after the last char
  63. char* ToUpper(const char* begin, const char* end, char* to) const;
  64. char* ToUpper(const char* begin, char* to) const;
  65. int stricmp(const char* s1, const char* s2) const;
  66. int strnicmp(const char* s1, const char* s2, size_t len) const;
  67. inline unsigned char ToUpper(unsigned char ch) const;
  68. inline unsigned char ToLower(unsigned char ch) const;
  69. inline unsigned char ToTitle(unsigned char ch) const;
  70. inline int ToDigit(unsigned char ch) const {
  71. return ::ToDigit(unicode[ch]);
  72. }
  73. static void Initialize();
  74. inline bool SingleByteCodepage() const {
  75. return DefaultChar != nullptr;
  76. }
  77. inline bool NativeCodepage() const {
  78. return SingleByteCodepage() || CPEnum == CODES_UTF8;
  79. }
  80. };
  81. class TCodePageHash;
  82. namespace NCodepagePrivate {
  83. class TCodepagesMap {
  84. private:
  85. static const int DataShift = 2;
  86. static const int DataSize = CODES_MAX + DataShift;
  87. const CodePage* Data[DataSize];
  88. private:
  89. inline const CodePage* GetPrivate(ECharset e) const {
  90. Y_ASSERT(e + DataShift >= 0 && e + DataShift < DataSize);
  91. return Data[e + DataShift];
  92. }
  93. void SetData(const CodePage* cp);
  94. public:
  95. TCodepagesMap();
  96. inline const CodePage* Get(ECharset e) const {
  97. const CodePage* res = GetPrivate(e);
  98. if (!res->SingleByteCodepage()) {
  99. ythrow yexception() << "CodePage (" << (int)e << ") structure can only be used for single byte encodings";
  100. }
  101. return res;
  102. }
  103. inline bool SingleByteCodepage(ECharset e) const {
  104. return GetPrivate(e)->SingleByteCodepage();
  105. }
  106. inline bool NativeCodepage(ECharset e) const {
  107. return GetPrivate(e)->NativeCodepage();
  108. }
  109. inline const char* NameByCharset(ECharset e) const {
  110. return GetPrivate(e)->Names[0];
  111. }
  112. static const TCodepagesMap& Instance();
  113. friend class ::TCodePageHash;
  114. };
  115. inline bool NativeCodepage(ECharset e) {
  116. return ::NCodepagePrivate::TCodepagesMap::Instance().NativeCodepage(e);
  117. }
  118. }
  119. inline bool SingleByteCodepage(ECharset e) {
  120. return ::NCodepagePrivate::TCodepagesMap::Instance().SingleByteCodepage(e);
  121. }
  122. inline bool ValidCodepage(ECharset e) {
  123. return e >= 0 && e < CODES_MAX;
  124. }
  125. inline const CodePage* CodePageByCharset(ECharset e) {
  126. return ::NCodepagePrivate::TCodepagesMap::Instance().Get(e);
  127. }
  128. ECharset CharsetByName(TStringBuf name);
  129. // Same as CharsetByName, but throws yexception() if name is invalid
  130. ECharset CharsetByNameOrDie(TStringBuf name);
  131. inline ECharset CharsetByCodePage(const CodePage* CP) {
  132. return CP->CPEnum;
  133. }
  134. inline const char* NameByCharset(ECharset e) {
  135. return ::NCodepagePrivate::TCodepagesMap::Instance().NameByCharset(e);
  136. }
  137. inline const char* NameByCharsetSafe(ECharset e) {
  138. if (CODES_UNKNOWN < e && e < CODES_MAX)
  139. return ::NCodepagePrivate::TCodepagesMap::Instance().NameByCharset(e);
  140. else
  141. ythrow yexception() << "unknown encoding: " << (int)e;
  142. }
  143. inline const char* NameByCodePage(const CodePage* CP) {
  144. return CP->Names[0];
  145. }
  146. inline const CodePage* CodePageByName(const char* name) {
  147. ECharset code = CharsetByName(name);
  148. if (code == CODES_UNKNOWN)
  149. return nullptr;
  150. return CodePageByCharset(code);
  151. }
  152. ECharset EncodingHintByName(const char* name);
  153. /*****************************************************************\
  154. * struct Encoder *
  155. \*****************************************************************/
  156. struct Encoder {
  157. char* Table[256];
  158. const char* DefaultChar;
  159. inline char Code(wchar32 ch) const {
  160. if (ch > 0xFFFF)
  161. return 0;
  162. return (unsigned char)Table[(ch >> 8) & 255][ch & 255];
  163. }
  164. inline char Tr(wchar32 ch) const {
  165. char code = Code(ch);
  166. if (code == 0 && ch != 0)
  167. code = DefaultChar[NUnicode::CharType(ch)];
  168. Y_ASSERT(code != 0 || ch == 0);
  169. return code;
  170. }
  171. inline unsigned char operator[](wchar32 ch) const {
  172. return Tr(ch);
  173. }
  174. void Tr(const wchar32* in, char* out, size_t len) const;
  175. void Tr(const wchar32* in, char* out) const;
  176. char* DefaultPlane;
  177. };
  178. /*****************************************************************\
  179. * struct Recoder *
  180. \*****************************************************************/
  181. struct Recoder {
  182. unsigned char Table[257];
  183. void Create(const CodePage& source, const CodePage& target);
  184. void Create(const CodePage& source, const Encoder* wideTarget);
  185. void Create(const CodePage& page, wchar32 (*mapper)(wchar32));
  186. void Create(const CodePage& page, const Encoder* widePage, wchar32 (*mapper)(wchar32));
  187. inline unsigned char Tr(unsigned char c) const {
  188. return Table[c];
  189. }
  190. inline unsigned char operator[](unsigned char c) const {
  191. return Table[c];
  192. }
  193. void Tr(const char* in, char* out, size_t len) const;
  194. void Tr(const char* in, char* out) const;
  195. void Tr(char* in_out, size_t len) const;
  196. void Tr(char* in_out) const;
  197. };
  198. extern const struct Encoder& WideCharToYandex;
  199. const Encoder& EncoderByCharset(ECharset enc);
  200. namespace NCodepagePrivate {
  201. class TCodePageData {
  202. private:
  203. static const CodePage* const AllCodePages[];
  204. static const Recoder rcdr_to_yandex[];
  205. static const Recoder rcdr_from_yandex[];
  206. static const Recoder rcdr_to_lower[];
  207. static const Recoder rcdr_to_upper[];
  208. static const Recoder rcdr_to_title[];
  209. static const Encoder* const EncodeTo[];
  210. friend struct ::CodePage;
  211. friend class TCodepagesMap;
  212. friend RECODE_RESULT _recodeToYandex(ECharset, const char*, char*, size_t, size_t, size_t&, size_t&);
  213. friend RECODE_RESULT _recodeFromYandex(ECharset, const char*, char*, size_t, size_t, size_t&, size_t&);
  214. friend const Encoder& ::EncoderByCharset(ECharset enc);
  215. };
  216. }
  217. inline const Encoder& EncoderByCharset(ECharset enc) {
  218. if (!SingleByteCodepage(enc)) {
  219. ythrow yexception() << "Encoder structure can only be used for single byte encodings";
  220. }
  221. return *NCodepagePrivate::TCodePageData::EncodeTo[enc];
  222. }
  223. inline unsigned char CodePage::ToUpper(unsigned char ch) const {
  224. return NCodepagePrivate::TCodePageData::rcdr_to_upper[CPEnum].Table[ch];
  225. }
  226. inline unsigned char CodePage::ToLower(unsigned char ch) const {
  227. return NCodepagePrivate::TCodePageData::rcdr_to_lower[CPEnum].Table[ch];
  228. }
  229. inline unsigned char CodePage::ToTitle(unsigned char ch) const {
  230. return NCodepagePrivate::TCodePageData::rcdr_to_title[CPEnum].Table[ch];
  231. }
  232. extern const CodePage& csYandex;
  233. /// these functions change (lowers) [end] position in case of utf-8
  234. /// null character is NOT assumed or written at [*end]
  235. void DecodeUnknownPlane(wchar16* start, wchar16*& end, const ECharset enc4unk);
  236. void DecodeUnknownPlane(wchar32* start, wchar32*& end, const ECharset enc4unk);
  237. inline void ToLower(char* s, size_t n, const CodePage& cp = csYandex) {
  238. char* const e = s + n;
  239. for (; s != e; ++s)
  240. *s = cp.ToLower(*s);
  241. }
  242. inline void ToUpper(char* s, size_t n, const CodePage& cp = csYandex) {
  243. char* const e = s + n;
  244. for (; s != e; ++s)
  245. *s = cp.ToUpper(*s);
  246. }
  247. inline TString ToLower(TString s, const CodePage& cp, size_t pos = 0, size_t n = TString::npos) {
  248. s.Transform([&cp](size_t, char c) { return cp.ToLower(c); }, pos, n);
  249. return s;
  250. }
  251. inline TString ToUpper(TString s, const CodePage& cp, size_t pos = 0, size_t n = TString::npos) {
  252. s.Transform([&cp](size_t, char c) { return cp.ToUpper(c); }, pos, n);
  253. return s;
  254. }
  255. inline TString ToTitle(TString s, const CodePage& cp, size_t pos = 0, size_t n = TString::npos) {
  256. s.Transform(
  257. [pos, &cp](size_t i, char c) {
  258. return i == pos ? cp.ToTitle(c) : cp.ToLower(c);
  259. },
  260. pos,
  261. n);
  262. return s;
  263. }