character.pas 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  1. unit character;
  2. interface
  3. {$ifndef VER2_4}
  4. {$mode objfpc}
  5. {$H+}
  6. {$PACKENUM 1}
  7. {$SCOPEDENUMS ON}
  8. type
  9. // Unicode General Category
  10. TUnicodeCategory = (
  11. ucUppercaseLetter, // Lu = Letter, uppercase
  12. ucLowercaseLetter, // Ll = Letter, lowercase
  13. ucTitlecaseLetter, // Lt = Letter, titlecase
  14. ucModifierLetter, // Lm = Letter, modifier
  15. ucOtherLetter, // Lo = Letter, other
  16. ucNonSpacingMark, // Mn = Mark, nonspacing
  17. ucCombiningMark, // Mc = Mark, spacing combining
  18. ucEnclosingMark, // Me = Mark, enclosing
  19. ucDecimalNumber, // Nd = Number, decimal digit
  20. ucLetterNumber, // Nl = Number, letter
  21. ucOtherNumber, // No = Number, other
  22. ucConnectPunctuation, // Pc = Punctuation, connector
  23. ucDashPunctuation, // Pd = Punctuation, dash
  24. ucOpenPunctuation, // Ps = Punctuation, open
  25. ucClosePunctuation, // Pe = Punctuation, close
  26. ucInitialPunctuation, // Pi = Punctuation, initial quote (may behave like Ps or Pe depending on usage)
  27. ucFinalPunctuation, // Pf = Punctuation, final quote (may behave like Ps or Pe depending on usage)
  28. ucOtherPunctuation, // Po = Punctuation, other
  29. ucMathSymbol, // Sm = Symbol, math
  30. ucCurrencySymbol, // Sc = Symbol, currency
  31. ucModifierSymbol, // Sk = Symbol, modifier
  32. ucOtherSymbol, // So = Symbol, other
  33. ucSpaceSeparator, // Zs = Separator, space
  34. ucLineSeparator, // Zl = Separator, line
  35. ucParagraphSeparator, // Zp = Separator, paragraph
  36. ucControl, // Cc = Other, control
  37. ucFormat, // Cf = Other, format
  38. ucSurrogate, // Cs = Other, surrogate
  39. ucPrivateUse, // Co = Other, private use
  40. ucUnassigned // Cn = Other, not assigned (including noncharacters)
  41. );
  42. TUnicodeCategorySet = set of TUnicodeCategory;
  43. { TCharacter }
  44. TCharacter = class sealed
  45. private
  46. class function TestCategory(const AString : UnicodeString; AIndex : Integer; ACategory : TUnicodeCategory) : Boolean; overload; static;
  47. class function TestCategory(const AString : UnicodeString; AIndex : Integer; ACategory : TUnicodeCategorySet) : Boolean; overload; static;
  48. public
  49. constructor Create;
  50. class function ConvertFromUtf32(AChar : UCS4Char) : UnicodeString; static;
  51. class function ConvertToUtf32(const AString : UnicodeString; AIndex : Integer) : UCS4Char; overload; static;
  52. class function ConvertToUtf32(const AString : UnicodeString; AIndex : Integer; out ACharLength : Integer) : UCS4Char; overload; static;
  53. class function ConvertToUtf32(const AHighSurrogate, ALowSurrogate : UnicodeChar) : UCS4Char; overload; static;
  54. class function GetNumericValue(AChar : UnicodeChar) : Double; static; overload;
  55. class function GetNumericValue(const AString : UnicodeString; AIndex : Integer) : Double; overload; static;
  56. class function GetUnicodeCategory(AChar : UnicodeChar) : TUnicodeCategory; overload; static; inline;
  57. class function GetUnicodeCategory(const AString : UnicodeString; AIndex : Integer) : TUnicodeCategory; overload; static;
  58. class function IsControl(AChar : UnicodeChar) : Boolean; overload; static; inline;
  59. class function IsControl(const AString : UnicodeString; AIndex : Integer) : Boolean; overload; static; inline;
  60. class function IsDigit(AChar : UnicodeChar) : Boolean; overload; static; inline;
  61. class function IsDigit(const AString : UnicodeString; AIndex : Integer) : Boolean; overload; static; inline;
  62. class function IsSurrogate(AChar : UnicodeChar) : Boolean; overload; static; inline;
  63. class function IsSurrogate(const AString : UnicodeString; AIndex : Integer) : Boolean; overload; static;
  64. class function IsHighSurrogate(AChar : UnicodeChar) : Boolean; overload; static; inline;
  65. class function IsHighSurrogate(const AString : UnicodeString; AIndex : Integer) : Boolean; overload; static;
  66. class function IsLowSurrogate(AChar : UnicodeChar) : Boolean; overload; static; inline;
  67. class function IsLowSurrogate(const AString : UnicodeString; AIndex : Integer) : Boolean; overload; static;
  68. class function IsSurrogatePair(const AHighSurrogate, ALowSurrogate : UnicodeChar) : Boolean; overload; static; inline; inline;
  69. class function IsSurrogatePair(const AString : UnicodeString; AIndex : Integer) : Boolean; overload; static;
  70. class function IsLetter(AChar : UnicodeChar) : Boolean; overload; static; inline;
  71. class function IsLetter(const AString : UnicodeString; AIndex : Integer) : Boolean; overload; static; inline;
  72. class function IsLetterOrDigit(AChar : UnicodeChar) : Boolean; overload; static; inline;
  73. class function IsLetterOrDigit(const AString : UnicodeString; AIndex : Integer) : Boolean; overload; static; inline;
  74. class function IsLower(AChar : UnicodeChar) : Boolean; overload; static; inline; inline;
  75. class function IsLower(const AString : UnicodeString; AIndex : Integer) : Boolean; overload; static; inline;
  76. class function IsNumber(AChar : UnicodeChar) : Boolean; overload; static; inline;
  77. class function IsNumber(const AString : UnicodeString; AIndex : Integer) : Boolean; overload; static;
  78. class function IsPunctuation(AChar : UnicodeChar) : Boolean; overload; static; inline;
  79. class function IsPunctuation(const AString : UnicodeString; AIndex : Integer) : Boolean; overload; static; inline;
  80. class function IsSeparator(AChar : UnicodeChar) : Boolean; overload; static; inline;
  81. class function IsSeparator(const AString : UnicodeString; AIndex : Integer) : Boolean; overload; static; inline;
  82. class function IsSymbol(AChar : UnicodeChar) : Boolean; overload; static; inline;
  83. class function IsSymbol(const AString : UnicodeString; AIndex : Integer) : Boolean; overload; static; inline;
  84. class function IsUpper(AChar : UnicodeChar) : Boolean; overload; static; inline;
  85. class function IsUpper(const AString : UnicodeString; AIndex : Integer) : Boolean; overload; static; inline;
  86. class function IsWhiteSpace(AChar : UnicodeChar) : Boolean; overload; static; inline;
  87. class function IsWhiteSpace(const AString : UnicodeString; AIndex : Integer) : Boolean; overload; static;
  88. class function ToLower(AChar : UnicodeChar) : UnicodeChar; overload; static;
  89. class function ToLower(const AString : UnicodeString) : UnicodeString; overload; static;
  90. class function ToUpper(AChar : UnicodeChar) : UnicodeChar; overload; static;
  91. class function ToUpper(const AString : UnicodeString) : UnicodeString; overload; static;
  92. end;
  93. // flat functions
  94. function ConvertFromUtf32(AChar : UCS4Char) : UnicodeString;
  95. function ConvertToUtf32(const AString : UnicodeString; AIndex : Integer) : UCS4Char; overload;
  96. function ConvertToUtf32(const AString : UnicodeString; AIndex : Integer; out ACharLength : Integer) : UCS4Char; overload;
  97. function ConvertToUtf32(const AHighSurrogate, ALowSurrogate : UnicodeChar) : UCS4Char; overload;
  98. function GetNumericValue(AChar : UnicodeChar) : Double; overload;
  99. function GetNumericValue(const AString : UnicodeString; AIndex : Integer) : Double; overload;
  100. function GetUnicodeCategory(AChar : UnicodeChar) : TUnicodeCategory; overload;
  101. function GetUnicodeCategory(const AString : UnicodeString; AIndex : Integer) : TUnicodeCategory; overload;
  102. function IsControl(AChar : UnicodeChar) : Boolean; overload;
  103. function IsControl(const AString : UnicodeString; AIndex : Integer) : Boolean; overload;
  104. function IsDigit(AChar : UnicodeChar) : Boolean; overload;
  105. function IsDigit(const AString : UnicodeString; AIndex : Integer) : Boolean; overload;
  106. function IsSurrogate(AChar : UnicodeChar) : Boolean; overload;
  107. function IsSurrogate(const AString : UnicodeString; AIndex : Integer) : Boolean; overload;
  108. function IsHighSurrogate(AChar : UnicodeChar) : Boolean; overload;
  109. function IsHighSurrogate(const AString : UnicodeString; AIndex : Integer) : Boolean; overload;
  110. function IsLowSurrogate(AChar : UnicodeChar) : Boolean; overload;
  111. function IsLowSurrogate(const AString : UnicodeString; AIndex : Integer) : Boolean; overload;
  112. function IsSurrogatePair(const AHighSurrogate, ALowSurrogate : UnicodeChar) : Boolean; overload;
  113. function IsSurrogatePair(const AString : UnicodeString; AIndex : Integer) : Boolean; overload;
  114. function IsLetter(AChar : UnicodeChar) : Boolean; overload;
  115. function IsLetter(const AString : UnicodeString; AIndex : Integer) : Boolean; overload;
  116. function IsLetterOrDigit(AChar : UnicodeChar) : Boolean; overload;
  117. function IsLetterOrDigit(const AString : UnicodeString; AIndex : Integer) : Boolean; overload;
  118. function IsLower(AChar : UnicodeChar) : Boolean; overload;
  119. function IsLower(const AString : UnicodeString; AIndex : Integer) : Boolean; overload;
  120. function IsNumber(AChar : UnicodeChar) : Boolean; overload;
  121. function IsNumber(const AString : UnicodeString; AIndex : Integer) : Boolean; overload;
  122. function IsPunctuation(AChar : UnicodeChar) : Boolean; overload;
  123. function IsPunctuation(const AString : UnicodeString; AIndex : Integer) : Boolean; overload;
  124. function IsSeparator(AChar : UnicodeChar) : Boolean; overload;
  125. function IsSeparator(const AString : UnicodeString; AIndex : Integer) : Boolean; overload;
  126. function IsSymbol(AChar : UnicodeChar) : Boolean; overload;
  127. function IsSymbol(const AString : UnicodeString; AIndex : Integer) : Boolean; overload;
  128. function IsUpper(AChar : UnicodeChar) : Boolean; overload;
  129. function IsUpper(const AString : UnicodeString; AIndex : Integer) : Boolean; overload;
  130. function IsWhiteSpace(AChar : UnicodeChar) : Boolean; overload;
  131. function IsWhiteSpace(const AString : UnicodeString; AIndex : Integer) : Boolean; overload;
  132. function ToLower(AChar : UnicodeChar) : UnicodeChar; overload;
  133. function ToLower(const AString : UnicodeString) : UnicodeString; overload;
  134. function ToUpper(AChar : UnicodeChar) : UnicodeChar; overload;
  135. function ToUpper(const AString : UnicodeString) : UnicodeString; overload;
  136. {$endif VER2_4}
  137. implementation
  138. {$ifndef VER2_4}
  139. uses
  140. SysUtils,
  141. RtlConsts;
  142. type
  143. PUC_Prop = ^TUC_Prop;
  144. TUC_Prop = packed record
  145. Category : TUnicodeCategory;
  146. NumericValue : Double;
  147. SimpleUpperCase : DWord;
  148. SimpleLowerCase : DWord;
  149. WhiteSpace : Boolean;
  150. end;
  151. {$INCLUDE unicodedata.inc} // For BMP code points
  152. {$INCLUDE unicodedata2.inc} // For other planes
  153. const
  154. LOW_SURROGATE_BEGIN = Word($DC00);
  155. LOW_SURROGATE_END = Word($DFFF);
  156. HIGH_SURROGATE_BEGIN = Word($D800);
  157. HIGH_SURROGATE_END = Word($DBFF);
  158. HIGH_SURROGATE_COUNT = HIGH_SURROGATE_END - HIGH_SURROGATE_BEGIN + 1;
  159. UCS4_HALF_BASE = LongWord($10000);
  160. UCS4_HALF_MASK = Word($3FF);
  161. MAX_LEGAL_UTF32 = $10FFFF;
  162. const
  163. LETTER_CATEGORIES = [
  164. TUnicodeCategory.ucUppercaseLetter, TUnicodeCategory.ucLowercaseLetter,
  165. TUnicodeCategory.ucTitlecaseLetter, TUnicodeCategory.ucModifierLetter,
  166. TUnicodeCategory.ucOtherLetter
  167. ];
  168. LETTER_OR_DIGIT_CATEGORIES =
  169. LETTER_CATEGORIES +
  170. [TUnicodeCategory.ucDecimalNumber,TUnicodeCategory.ucLetterNumber];
  171. NUMBER_CATEGORIES =
  172. [ TUnicodeCategory.ucDecimalNumber, TUnicodeCategory.ucLetterNumber,
  173. TUnicodeCategory.ucOtherNumber
  174. ];
  175. PUNCTUATION_CATEGORIES = [
  176. TUnicodeCategory.ucConnectPunctuation, TUnicodeCategory.ucDashPunctuation,
  177. TUnicodeCategory.ucOpenPunctuation, TUnicodeCategory.ucClosePunctuation,
  178. TUnicodeCategory.ucInitialPunctuation, TUnicodeCategory.ucFinalPunctuation,
  179. TUnicodeCategory.ucOtherPunctuation
  180. ];
  181. SEPARATOR_CATEGORIES =
  182. [ TUnicodeCategory.ucSpaceSeparator, TUnicodeCategory.ucLineSeparator,
  183. TUnicodeCategory.ucParagraphSeparator
  184. ];
  185. SYMBOL_CATEGORIES =
  186. [ TUnicodeCategory.ucMathSymbol, TUnicodeCategory.ucCurrencySymbol,
  187. TUnicodeCategory.ucModifierSymbol, TUnicodeCategory.ucOtherSymbol
  188. ];
  189. function GetProps(const ACodePoint : Word) : PUC_Prop; inline;
  190. begin
  191. Result:=
  192. @UC_PROP_ARRAY[
  193. UC_TABLE_2[
  194. (UC_TABLE_1[WordRec(ACodePoint).Hi] * 256) +
  195. WordRec(ACodePoint).Lo
  196. ]
  197. ];
  198. end;
  199. function GetProps(const AHighS, ALowS : UnicodeChar): PUC_Prop; inline;
  200. begin
  201. Result:=
  202. @UC_PROP_ARRAY[
  203. UCO_TABLE_2[
  204. (UCO_TABLE_1[Word(AHighS)-HIGH_SURROGATE_BEGIN] * HIGH_SURROGATE_COUNT) +
  205. Word(ALowS) - LOW_SURROGATE_BEGIN
  206. ]
  207. ];
  208. end;
  209. procedure FromUCS4(const AValue : UCS4Char; var AHighS, ALowS : UnicodeChar);
  210. begin
  211. AHighS := UnicodeChar((AValue - $10000) shr 10 + $d800);
  212. ALowS := UnicodeChar((AValue - $10000) and $3ff + $dc00);
  213. end;
  214. function ConvertFromUtf32(AChar: UCS4Char): UnicodeString;
  215. begin
  216. Result := TCharacter.ConvertFromUtf32(AChar);
  217. end;
  218. function ConvertToUtf32(const AString: UnicodeString; AIndex: Integer): UCS4Char;
  219. begin
  220. Result := TCharacter.ConvertToUtf32(AString, AIndex);
  221. end;
  222. function ConvertToUtf32(const AString: UnicodeString; AIndex: Integer; out ACharLength: Integer): UCS4Char;
  223. begin
  224. Result := TCharacter.ConvertToUtf32(AString, AIndex, ACharLength);
  225. end;
  226. function ConvertToUtf32(const AHighSurrogate, ALowSurrogate: UnicodeChar): UCS4Char;
  227. begin
  228. Result := TCharacter.ConvertToUtf32(AHighSurrogate, ALowSurrogate);
  229. end;
  230. function GetNumericValue(AChar: UnicodeChar): Double;
  231. begin
  232. Result := TCharacter.GetNumericValue(AChar);
  233. end;
  234. function GetNumericValue(const AString: UnicodeString; AIndex: Integer): Double;
  235. begin
  236. Result := TCharacter.GetNumericValue(AString, AIndex);
  237. end;
  238. function GetUnicodeCategory(AChar: UnicodeChar): TUnicodeCategory;
  239. begin
  240. Result := TCharacter.GetUnicodeCategory(AChar);
  241. end;
  242. function GetUnicodeCategory(const AString: UnicodeString; AIndex: Integer): TUnicodeCategory;
  243. begin
  244. Result := TCharacter.GetUnicodeCategory(AString, AIndex);
  245. end;
  246. function IsControl(AChar: UnicodeChar): Boolean;
  247. begin
  248. Result := TCharacter.IsControl(AChar);
  249. end;
  250. function IsControl(const AString: UnicodeString; AIndex: Integer): Boolean;
  251. begin
  252. Result := TCharacter.IsControl(AString, AIndex);
  253. end;
  254. function IsDigit(AChar: UnicodeChar): Boolean;
  255. begin
  256. Result := TCharacter.IsDigit(AChar);
  257. end;
  258. function IsDigit(const AString: UnicodeString; AIndex: Integer): Boolean;
  259. begin
  260. Result := TCharacter.IsDigit(AString, AIndex);
  261. end;
  262. function IsSurrogate(AChar: UnicodeChar): Boolean;
  263. begin
  264. Result := TCharacter.IsSurrogate(AChar);
  265. end;
  266. function IsSurrogate(const AString: UnicodeString; AIndex: Integer): Boolean;
  267. begin
  268. Result := TCharacter.IsSurrogate(AString, AIndex);
  269. end;
  270. function IsHighSurrogate(AChar: UnicodeChar): Boolean;
  271. begin
  272. Result := TCharacter.IsHighSurrogate(AChar);
  273. end;
  274. function IsHighSurrogate(const AString: UnicodeString; AIndex: Integer): Boolean;
  275. begin
  276. Result := TCharacter.IsHighSurrogate(AString, AIndex);
  277. end;
  278. function IsLowSurrogate(AChar: UnicodeChar): Boolean;
  279. begin
  280. Result := TCharacter.IsLowSurrogate(AChar);
  281. end;
  282. function IsLowSurrogate(const AString: UnicodeString; AIndex: Integer): Boolean;
  283. begin
  284. Result := TCharacter.IsLowSurrogate(AString, AIndex);
  285. end;
  286. function IsSurrogatePair(const AHighSurrogate, ALowSurrogate: UnicodeChar): Boolean;
  287. begin
  288. Result := TCharacter.IsSurrogatePair(AHighSurrogate, ALowSurrogate);
  289. end;
  290. function IsSurrogatePair(const AString: UnicodeString; AIndex: Integer): Boolean;
  291. begin
  292. Result := TCharacter.IsSurrogatePair(AString, AIndex);
  293. end;
  294. function IsLetter(AChar: UnicodeChar): Boolean;
  295. begin
  296. Result := TCharacter.IsLetter(AChar);
  297. end;
  298. function IsLetter(const AString: UnicodeString; AIndex: Integer): Boolean;
  299. begin
  300. Result := TCharacter.IsLetter(AString, AIndex);
  301. end;
  302. function IsLetterOrDigit(AChar: UnicodeChar): Boolean;
  303. begin
  304. Result := TCharacter.IsLetterOrDigit(AChar);
  305. end;
  306. function IsLetterOrDigit(const AString: UnicodeString; AIndex: Integer): Boolean;
  307. begin
  308. Result := TCharacter.IsLetterOrDigit(AString, AIndex);
  309. end;
  310. function IsLower(AChar: UnicodeChar): Boolean;
  311. begin
  312. Result := TCharacter.IsLower(AChar);
  313. end;
  314. function IsLower(const AString: UnicodeString; AIndex: Integer): Boolean;
  315. begin
  316. Result := TCharacter.IsLower(AString, AIndex);
  317. end;
  318. function IsNumber(AChar: UnicodeChar): Boolean;
  319. begin
  320. Result := TCharacter.IsNumber(AChar);
  321. end;
  322. function IsNumber(const AString: UnicodeString; AIndex: Integer): Boolean;
  323. begin
  324. Result := TCharacter.IsNumber(AString, AIndex);
  325. end;
  326. function IsPunctuation(AChar: UnicodeChar): Boolean;
  327. begin
  328. Result := TCharacter.IsPunctuation(AChar);
  329. end;
  330. function IsPunctuation(const AString: UnicodeString; AIndex: Integer): Boolean;
  331. begin
  332. Result := TCharacter.IsPunctuation(AString, AIndex);
  333. end;
  334. function IsSeparator(AChar: UnicodeChar): Boolean;
  335. begin
  336. Result := TCharacter.IsSeparator(AChar);
  337. end;
  338. function IsSeparator(const AString: UnicodeString; AIndex: Integer): Boolean;
  339. begin
  340. Result := TCharacter.IsSeparator(AString, AIndex);
  341. end;
  342. function IsSymbol(AChar: UnicodeChar): Boolean;
  343. begin
  344. Result := TCharacter.IsSymbol(AChar);
  345. end;
  346. function IsSymbol(const AString: UnicodeString; AIndex: Integer): Boolean;
  347. begin
  348. Result := TCharacter.IsSymbol(AString, AIndex);
  349. end;
  350. function IsUpper(AChar: UnicodeChar): Boolean;
  351. begin
  352. Result := TCharacter.IsUpper(AChar);
  353. end;
  354. function IsUpper(const AString: UnicodeString; AIndex: Integer): Boolean;
  355. begin
  356. Result := TCharacter.IsUpper(AString, AIndex);
  357. end;
  358. function IsWhiteSpace(AChar: UnicodeChar): Boolean;
  359. begin
  360. Result := TCharacter.IsWhiteSpace(AChar);
  361. end;
  362. function IsWhiteSpace(const AString: UnicodeString; AIndex: Integer): Boolean;
  363. begin
  364. Result := TCharacter.IsWhiteSpace(AString, AIndex);
  365. end;
  366. function ToLower(AChar: UnicodeChar): UnicodeChar;
  367. begin
  368. Result := TCharacter.ToLower(AChar);
  369. end;
  370. function ToLower(const AString: UnicodeString): UnicodeString;
  371. begin
  372. Result := TCharacter.ToLower(AString);
  373. end;
  374. function ToUpper(AChar: UnicodeChar): UnicodeChar;
  375. begin
  376. Result := TCharacter.ToUpper(AChar);
  377. end;
  378. function ToUpper(const AString: UnicodeString): UnicodeString;
  379. begin
  380. Result := TCharacter.ToUpper(AString);
  381. end;
  382. { TCharacter }
  383. class function TCharacter.TestCategory(
  384. const AString : UnicodeString;
  385. AIndex : Integer;
  386. ACategory : TUnicodeCategory
  387. ) : Boolean;
  388. var
  389. pu : PUC_Prop;
  390. begin
  391. if (AIndex < 1) or (AIndex > Length(AString)) then
  392. raise EArgumentOutOfRangeException.CreateFmt(SStringIndexOutOfRange, [AIndex, Length(AString)]);
  393. pu := GetProps(Word(AString[AIndex]));
  394. if (pu^.Category = TUnicodeCategory.ucSurrogate) then begin
  395. if not IsSurrogatePair(AString,AIndex) then
  396. raise EArgumentException.Create(SInvalidUnicodeCodePointSequence);
  397. pu := GetProps(AString[AIndex],AString[AIndex+1]);
  398. end;
  399. Result := (pu^.Category = ACategory);
  400. end;
  401. class function TCharacter.TestCategory(
  402. const AString : UnicodeString;
  403. AIndex : Integer;
  404. ACategory : TUnicodeCategorySet
  405. ) : Boolean;
  406. var
  407. pu : PUC_Prop;
  408. begin
  409. if (AIndex < 1) or (AIndex > Length(AString)) then
  410. raise EArgumentOutOfRangeException.CreateFmt(SStringIndexOutOfRange, [AIndex, Length(AString)]);
  411. pu := GetProps(Word(AString[AIndex]));
  412. if (pu^.Category = TUnicodeCategory.ucSurrogate) then begin
  413. if not IsSurrogatePair(AString,AIndex) then
  414. raise EArgumentException.Create(SInvalidUnicodeCodePointSequence);
  415. pu := GetProps(AString[AIndex],AString[AIndex+1]);
  416. end;
  417. Result := (pu^.Category in ACategory);
  418. end;
  419. constructor TCharacter.Create;
  420. begin
  421. raise ENoConstructException.CreateFmt(SClassCantBeConstructed, [ClassName]);
  422. end;
  423. class function TCharacter.ConvertFromUtf32(AChar : UCS4Char) : UnicodeString;
  424. begin
  425. if AChar < UCS4_HALF_BASE then
  426. begin
  427. if IsSurrogate(UnicodeChar(AChar)) then
  428. raise EArgumentOutOfRangeException.CreateFmt(SInvalidUTF32Char, [AChar]);
  429. Result := UnicodeChar(AChar);
  430. end
  431. else
  432. begin
  433. if AChar > MAX_LEGAL_UTF32 then
  434. raise EArgumentOutOfRangeException.CreateFmt(SInvalidUTF32Char, [AChar]);
  435. SetLength(Result, 2);
  436. AChar := AChar - UCS4_HALF_BASE;
  437. Result[1] := UnicodeChar((AChar shr 10) + HIGH_SURROGATE_BEGIN);
  438. Result[2] := UnicodeChar((AChar and UCS4_HALF_MASK) + LOW_SURROGATE_BEGIN);
  439. end;
  440. end;
  441. class function TCharacter.ConvertToUtf32(const AString : UnicodeString; AIndex : Integer) : UCS4Char; overload;
  442. begin
  443. if (AIndex < 1) or (AIndex > Length(AString)) then
  444. raise EArgumentOutOfRangeException.CreateFmt(SStringIndexOutOfRange, [AIndex, Length(AString)]);
  445. Result := Word(AString[AIndex]);
  446. if IsHighSurrogate(UnicodeChar(Result)) then
  447. begin
  448. if Length(AString) < Succ(AIndex) then
  449. raise EArgumentException.CreateFmt(SInvalidHighSurrogate, [AIndex]);
  450. Result := ConvertToUtf32(UnicodeChar(Result), AString[Succ(AIndex)]);
  451. end;
  452. end;
  453. class function TCharacter.ConvertToUtf32(const AString : UnicodeString; AIndex : Integer; out ACharLength : Integer) : UCS4Char; overload;
  454. begin
  455. if (AIndex < 1) or (AIndex > Length(AString)) then
  456. raise EArgumentOutOfRangeException.CreateFmt(SStringIndexOutOfRange, [AIndex, Length(AString)]);
  457. Result := Word(AString[AIndex]);
  458. if IsHighSurrogate(UnicodeChar(Result)) then
  459. begin
  460. if Length(AString) < Succ(AIndex) then
  461. raise EArgumentException.CreateFmt(SInvalidHighSurrogate, [AIndex]);
  462. Result := ConvertToUtf32(UnicodeChar(Result), AString[Succ(AIndex)]);
  463. ACharLength := 2;
  464. end
  465. else
  466. ACharLength := 1;
  467. end;
  468. class function TCharacter.ConvertToUtf32(const AHighSurrogate, ALowSurrogate : UnicodeChar) : UCS4Char; overload;
  469. begin
  470. if not IsHighSurrogate(AHighSurrogate) then
  471. raise EArgumentOutOfRangeException.CreateFmt(SHighSurrogateOutOfRange, [Word(AHighSurrogate)]);
  472. if not IsLowSurrogate(ALowSurrogate) then
  473. raise EArgumentOutOfRangeException.CreateFmt(SLowSurrogateOutOfRange, [Word(ALowSurrogate)]);
  474. Result := (UCS4Char(AHighSurrogate) - HIGH_SURROGATE_BEGIN) shl 10 + (UCS4Char(ALowSurrogate) - LOW_SURROGATE_BEGIN) + UCS4_HALF_BASE;
  475. end;
  476. class function TCharacter.GetNumericValue(AChar : UnicodeChar) : Double;
  477. begin
  478. Result := GetProps(Word(AChar))^.NumericValue;
  479. end;
  480. class function TCharacter.GetNumericValue(
  481. const AString : UnicodeString;
  482. AIndex : Integer
  483. ) : Double;
  484. var
  485. pu : PUC_Prop;
  486. begin
  487. if (AIndex < 1) or (AIndex > Length(AString)) then
  488. raise EArgumentOutOfRangeException.CreateFmt(SStringIndexOutOfRange, [AIndex, Length(AString)]);
  489. pu := GetProps(Word(AString[AIndex]));
  490. if (pu^.Category = TUnicodeCategory.ucSurrogate) then begin
  491. if not IsSurrogatePair(AString,AIndex) then
  492. raise EArgumentException.Create(SInvalidUnicodeCodePointSequence);
  493. pu := GetProps(AString[AIndex],AString[AIndex+1]);
  494. end;
  495. Result := pu^.NumericValue;
  496. end;
  497. class function TCharacter.GetUnicodeCategory(AChar : UnicodeChar) : TUnicodeCategory;
  498. begin
  499. Result := GetProps(Word(AChar))^.Category;
  500. end;
  501. class function TCharacter.GetUnicodeCategory(
  502. const AString : UnicodeString;
  503. AIndex : Integer
  504. ) : TUnicodeCategory;
  505. var
  506. pu : PUC_Prop;
  507. begin
  508. if (AIndex < 1) or (AIndex > Length(AString)) then
  509. raise EArgumentOutOfRangeException.CreateFmt(SStringIndexOutOfRange, [AIndex, Length(AString)]);
  510. pu := GetProps(Word(AString[AIndex]));
  511. if (pu^.Category = TUnicodeCategory.ucSurrogate) then begin
  512. if not IsSurrogatePair(AString,AIndex) then
  513. raise EArgumentException.Create(SInvalidUnicodeCodePointSequence);
  514. pu := GetProps(AString[AIndex],AString[AIndex+1]);
  515. end;
  516. Result := pu^.Category;
  517. end;
  518. class function TCharacter.IsControl(AChar : UnicodeChar) : Boolean;
  519. begin
  520. Result := (GetProps(Word(AChar))^.Category = TUnicodeCategory.ucControl);
  521. end;
  522. class function TCharacter.IsControl(
  523. const AString : UnicodeString;
  524. AIndex : Integer
  525. ) : Boolean;
  526. begin
  527. Result := TestCategory(AString,AIndex,TUnicodeCategory.ucControl);
  528. end;
  529. class function TCharacter.IsDigit(AChar : UnicodeChar) : Boolean;
  530. begin
  531. Result := (GetProps(Word(AChar))^.Category = TUnicodeCategory.ucDecimalNumber);
  532. end;
  533. class function TCharacter.IsDigit(
  534. const AString : UnicodeString;
  535. AIndex : Integer
  536. ) : Boolean;
  537. begin
  538. Result := TestCategory(AString,AIndex,TUnicodeCategory.ucDecimalNumber);
  539. end;
  540. class function TCharacter.IsSurrogate(AChar : UnicodeChar) : Boolean;
  541. begin
  542. Result := (GetProps(Word(AChar))^.Category = TUnicodeCategory.ucSurrogate);
  543. end;
  544. class function TCharacter.IsSurrogate(
  545. const AString : UnicodeString;
  546. AIndex : Integer
  547. ) : Boolean;
  548. begin
  549. if (AIndex < 1) or (AIndex > Length(AString)) then
  550. raise EArgumentOutOfRangeException.CreateFmt(SStringIndexOutOfRange, [AIndex, Length(AString)]);
  551. Result := IsSurrogate(AString[AIndex]);
  552. end;
  553. class function TCharacter.IsHighSurrogate(AChar : UnicodeChar) : Boolean;
  554. begin
  555. Result := (GetProps(Word(AChar))^.Category = TUnicodeCategory.ucSurrogate) and
  556. (Word(AChar) >= HIGH_SURROGATE_BEGIN) and
  557. (Word(AChar) <= HIGH_SURROGATE_END);
  558. end;
  559. class function TCharacter.IsHighSurrogate(
  560. const AString : UnicodeString;
  561. AIndex : Integer
  562. ) : Boolean;
  563. begin
  564. if (AIndex < 1) or (AIndex > Length(AString)) then
  565. raise EArgumentOutOfRangeException.CreateFmt(SStringIndexOutOfRange, [AIndex, Length(AString)]);
  566. Result := IsHighSurrogate(AString[AIndex]);
  567. end;
  568. class function TCharacter.IsLowSurrogate(AChar : UnicodeChar) : Boolean;
  569. begin
  570. Result := (GetProps(Word(AChar))^.Category = TUnicodeCategory.ucSurrogate) and
  571. (Word(AChar) >= LOW_SURROGATE_BEGIN) and
  572. (Word(AChar) <= LOW_SURROGATE_END);
  573. end;
  574. class function TCharacter.IsLowSurrogate(
  575. const AString : UnicodeString;
  576. AIndex : Integer
  577. ) : Boolean;
  578. begin
  579. if (AIndex < 1) or (AIndex > Length(AString)) then
  580. raise EArgumentOutOfRangeException.CreateFmt(SStringIndexOutOfRange, [AIndex, Length(AString)]);
  581. Result := IsLowSurrogate(AString[AIndex]);
  582. end;
  583. class function TCharacter.IsSurrogatePair(
  584. const AHighSurrogate,
  585. ALowSurrogate : UnicodeChar
  586. ) : Boolean;
  587. begin
  588. Result :=
  589. ( (Word(AHighSurrogate) >= HIGH_SURROGATE_BEGIN) and
  590. (Word(AHighSurrogate) <= HIGH_SURROGATE_END)
  591. ) and
  592. ( (Word(ALowSurrogate) >= LOW_SURROGATE_BEGIN) and
  593. (Word(ALowSurrogate) <= LOW_SURROGATE_END)
  594. )
  595. end;
  596. class function TCharacter.IsSurrogatePair(
  597. const AString : UnicodeString;
  598. AIndex : Integer
  599. ) : Boolean;
  600. begin
  601. if (AIndex < 1) or (AIndex > Length(AString)) then
  602. raise EArgumentOutOfRangeException.CreateFmt(SStringIndexOutOfRange, [AIndex, Length(AString)]);
  603. if not IsHighSurrogate(AString[AIndex]) then begin
  604. Result := False;
  605. exit;
  606. end;
  607. if ((AIndex+1) > Length(AString)) then
  608. raise EArgumentOutOfRangeException.CreateFmt(SStringIndexOutOfRange, [AIndex+1, Length(AString)]);
  609. Result := IsSurrogatePair(AString[AIndex],AString[AIndex+1]);
  610. end;
  611. class function TCharacter.IsLetter(AChar : UnicodeChar) : Boolean;
  612. begin
  613. Result := (GetProps(Word(AChar))^.Category in LETTER_CATEGORIES);
  614. end;
  615. class function TCharacter.IsLetter(
  616. const AString : UnicodeString;
  617. AIndex : Integer
  618. ) : Boolean;
  619. begin
  620. Result := TestCategory(AString,AIndex,LETTER_CATEGORIES);
  621. end;
  622. class function TCharacter.IsLetterOrDigit(AChar : UnicodeChar) : Boolean;
  623. begin
  624. Result := (GetProps(Word(AChar))^.Category in LETTER_OR_DIGIT_CATEGORIES);
  625. end;
  626. class function TCharacter.IsLetterOrDigit(
  627. const AString : UnicodeString;
  628. AIndex : Integer
  629. ) : Boolean;
  630. begin
  631. Result := TestCategory(AString,AIndex,LETTER_OR_DIGIT_CATEGORIES);
  632. end;
  633. class function TCharacter.IsLower(AChar : UnicodeChar) : Boolean;
  634. begin
  635. Result := (GetProps(Word(AChar))^.Category = TUnicodeCategory.ucLowercaseLetter);
  636. end;
  637. class function TCharacter.IsLower(
  638. const AString : UnicodeString;
  639. AIndex : Integer
  640. ) : Boolean;
  641. begin
  642. Result := TestCategory(AString,AIndex,TUnicodeCategory.ucLowercaseLetter);
  643. end;
  644. class function TCharacter.IsNumber(AChar : UnicodeChar) : Boolean;
  645. begin
  646. Result := (GetProps(Word(AChar))^.Category in NUMBER_CATEGORIES);
  647. end;
  648. class function TCharacter.IsNumber(
  649. const AString : UnicodeString;
  650. AIndex : Integer
  651. ) : Boolean;
  652. begin
  653. Result := TestCategory(AString,AIndex,NUMBER_CATEGORIES);
  654. end;
  655. class function TCharacter.IsPunctuation(AChar : UnicodeChar) : Boolean;
  656. begin
  657. Result := (GetProps(Word(AChar))^.Category in PUNCTUATION_CATEGORIES);
  658. end;
  659. class function TCharacter.IsPunctuation(
  660. const AString : UnicodeString;
  661. AIndex : Integer
  662. ) : Boolean;
  663. begin
  664. Result := TestCategory(AString,AIndex,PUNCTUATION_CATEGORIES);
  665. end;
  666. class function TCharacter.IsSeparator(AChar: UnicodeChar): Boolean;
  667. begin
  668. Result := (GetProps(Word(AChar))^.Category in SEPARATOR_CATEGORIES);
  669. end;
  670. class function TCharacter.IsSeparator(
  671. const AString : UnicodeString;
  672. AIndex : Integer
  673. ) : Boolean;
  674. begin
  675. Result := TestCategory(AString,AIndex,SEPARATOR_CATEGORIES);
  676. end;
  677. class function TCharacter.IsSymbol(AChar: UnicodeChar): Boolean;
  678. begin
  679. Result := (GetProps(Word(AChar))^.Category in SYMBOL_CATEGORIES);
  680. end;
  681. class function TCharacter.IsSymbol(
  682. const AString : UnicodeString;
  683. AIndex : Integer
  684. ) : Boolean;
  685. begin
  686. Result := TestCategory(AString,AIndex,SYMBOL_CATEGORIES);
  687. end;
  688. class function TCharacter.IsUpper(AChar : UnicodeChar) : Boolean;
  689. begin
  690. Result := (GetProps(Word(AChar))^.Category = TUnicodeCategory.ucUppercaseLetter);
  691. end;
  692. class function TCharacter.IsUpper(
  693. const AString : UnicodeString;
  694. AIndex : Integer
  695. ) : Boolean;
  696. begin
  697. Result := TestCategory(AString,AIndex,TUnicodeCategory.ucUppercaseLetter);
  698. end;
  699. class function TCharacter.IsWhiteSpace(AChar : UnicodeChar) : Boolean;
  700. begin
  701. Result := GetProps(Word(AChar))^.WhiteSpace;
  702. end;
  703. class function TCharacter.IsWhiteSpace(
  704. const AString : UnicodeString;
  705. AIndex : Integer
  706. ) : Boolean;
  707. var
  708. pu : PUC_Prop;
  709. begin
  710. if (AIndex < 1) or (AIndex > Length(AString)) then
  711. raise EArgumentOutOfRangeException.CreateFmt(SStringIndexOutOfRange, [AIndex, Length(AString)]);
  712. pu := GetProps(Word(AString[AIndex]));
  713. if (pu^.Category = TUnicodeCategory.ucSurrogate) then begin
  714. if not IsSurrogatePair(AString,AIndex) then
  715. raise EArgumentException.Create(SInvalidUnicodeCodePointSequence);
  716. pu := GetProps(AString[AIndex],AString[AIndex+1]);
  717. end;
  718. Result := pu^.WhiteSpace;
  719. end;
  720. class function TCharacter.ToLower(AChar : UnicodeChar) : UnicodeChar;
  721. begin
  722. Result := UnicodeChar(GetProps(Word(AChar))^.SimpleLowerCase);
  723. if (Result = UnicodeChar(0)) then
  724. Result := AChar;
  725. end;
  726. class function TCharacter.ToLower(const AString : UnicodeString) : UnicodeString;
  727. var
  728. i, c : SizeInt;
  729. pp, pr : PUnicodeChar;
  730. pu : PUC_Prop;
  731. locIsSurrogate : Boolean;
  732. begin
  733. c := Length(AString);
  734. SetLength(Result,2*c);
  735. if (c > 0) then begin
  736. pp := @AString[1];
  737. pr := @Result[1];
  738. i := 1;
  739. while (i <= c) do begin
  740. pu := GetProps(Word(pp^));
  741. locIsSurrogate := (pu^.Category = TUnicodeCategory.ucSurrogate);
  742. if locIsSurrogate then begin
  743. if not IsSurrogatePair(AString,i) then
  744. raise EArgumentException.Create(SInvalidUnicodeCodePointSequence);
  745. pu := GetProps(pp^,AString[i+1]);
  746. end;
  747. if (pu^.SimpleLowerCase = 0) then begin
  748. pr^ := pp^;
  749. if locIsSurrogate then begin
  750. Inc(pp);
  751. Inc(pr);
  752. Inc(i);
  753. pr^ := pp^;
  754. end;
  755. end else begin
  756. if (pu^.SimpleLowerCase <= $FFFF) then begin
  757. pr^ := UnicodeChar(Word(pu^.SimpleLowerCase));
  758. end else begin
  759. FromUCS4(UCS4Char(pu^.SimpleLowerCase),pr^,PUnicodeChar(PtrUInt(pr)+SizeOf(UnicodeChar))^);
  760. Inc(pr);
  761. end;
  762. if locIsSurrogate then begin
  763. Inc(pp);
  764. Inc(i);
  765. end;
  766. end;
  767. Inc(pp);
  768. Inc(pr);
  769. Inc(i);
  770. end;
  771. Dec(pp);
  772. i := ((PtrUInt(pr) - PtrUInt(@Result[1])) div SizeOf(UnicodeChar));
  773. SetLength(Result,i)
  774. end;
  775. end;
  776. class function TCharacter.ToUpper(AChar : UnicodeChar) : UnicodeChar;
  777. begin
  778. Result := UnicodeChar(GetProps(Word(AChar))^.SimpleUpperCase);
  779. if (Result = UnicodeChar(0)) then
  780. Result := AChar;
  781. end;
  782. class function TCharacter.ToUpper(const AString : UnicodeString) : UnicodeString;
  783. var
  784. i, c : SizeInt;
  785. pp, pr : PUnicodeChar;
  786. pu : PUC_Prop;
  787. locIsSurrogate : Boolean;
  788. begin
  789. c := Length(AString);
  790. SetLength(Result,2*c);
  791. if (c > 0) then begin
  792. pp := @AString[1];
  793. pr := @Result[1];
  794. i := 1;
  795. while (i <= c) do begin
  796. pu := GetProps(Word(pp^));
  797. locIsSurrogate := (pu^.Category = TUnicodeCategory.ucSurrogate);
  798. if locIsSurrogate then begin
  799. if not IsSurrogatePair(AString,i) then
  800. raise EArgumentException.Create(SInvalidUnicodeCodePointSequence);
  801. pu := GetProps(pp^,AString[i+1]);
  802. end;
  803. if (pu^.SimpleUpperCase = 0) then begin
  804. pr^ := pp^;
  805. if locIsSurrogate then begin
  806. Inc(pp);
  807. Inc(pr);
  808. Inc(i);
  809. pr^ := pp^;
  810. end;
  811. end else begin
  812. if (pu^.SimpleUpperCase <= $FFFF) then begin
  813. pr^ := UnicodeChar(Word(pu^.SimpleUpperCase));
  814. end else begin
  815. FromUCS4(UCS4Char(pu^.SimpleUpperCase),pr^,PUnicodeChar(PtrUInt(pr)+SizeOf(UnicodeChar))^);
  816. Inc(pr);
  817. end;
  818. if locIsSurrogate then begin
  819. Inc(pp);
  820. Inc(i);
  821. end;
  822. end;
  823. Inc(pp);
  824. Inc(pr);
  825. Inc(i);
  826. end;
  827. Dec(pp);
  828. i := ((PtrUInt(pr) - PtrUInt(@Result[1])) div SizeOf(UnicodeChar));
  829. SetLength(Result,i)
  830. end;
  831. end;
  832. {$endif VER2_4}
  833. end.