widestr.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. {
  2. Copyright (c) 2000-2002 by Florian Klaempfl
  3. This unit contains basic functions for unicode support in the
  4. compiler, this unit is mainly necessary to bootstrap widestring
  5. support ...
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. unit widestr;
  20. {$i fpcdefs.inc}
  21. interface
  22. uses
  23. {$if FPC_FULLVERSION<20700}ccharset{$else}charset{$endif},globtype;
  24. type
  25. tcompilerwidechar = word;
  26. tcompilerwidecharptr = ^tcompilerwidechar;
  27. pcompilerwidechar = ^tcompilerwidechar;
  28. pcompilerwidestring = ^_tcompilerwidestring;
  29. _tcompilerwidestring = record
  30. data : pcompilerwidechar;
  31. maxlen,len : SizeInt;
  32. end;
  33. procedure initwidestring(out r : pcompilerwidestring);
  34. procedure donewidestring(var r : pcompilerwidestring);
  35. procedure setlengthwidestring(r : pcompilerwidestring;l : SizeInt);
  36. function getlengthwidestring(r : pcompilerwidestring) : SizeInt;
  37. procedure concatwidestringchar(r : pcompilerwidestring;c : tcompilerwidechar);
  38. procedure concatwidestrings(s1,s2 : pcompilerwidestring);
  39. function comparewidestrings(s1,s2 : pcompilerwidestring) : SizeInt;
  40. procedure copywidestring(s,d : pcompilerwidestring);
  41. function asciichar2unicode(c : char) : tcompilerwidechar;
  42. function unicode2asciichar(c : tcompilerwidechar) : char;
  43. procedure ascii2unicode(p : pchar;l : SizeInt;cp : tstringencoding;r : pcompilerwidestring;codepagetranslation : boolean = true);
  44. procedure unicode2ascii(r : pcompilerwidestring;p : pchar;cp : tstringencoding);
  45. function hasnonasciichars(const p: pcompilerwidestring): boolean;
  46. function getcharwidestring(r : pcompilerwidestring;l : SizeInt) : tcompilerwidechar;
  47. function cpavailable(const s: string) : boolean;
  48. function cpavailable(cp: word) : boolean;
  49. procedure changecodepage(
  50. s : pchar; l : SizeInt; scp : tstringencoding;
  51. d : pchar; dcp : tstringencoding
  52. );
  53. function codepagebyname(const s : string) : tstringencoding;
  54. function charlength(p: pchar; len: sizeint): sizeint;
  55. function charlength(const s: string): sizeint;
  56. implementation
  57. uses
  58. {$if FPC_FULLVERSION>20700}
  59. { use only small codepage maps, others will be }
  60. { loaded on demand from -FM path }
  61. { cyrillic code pages }
  62. cp1251,cp866,cp8859_5,
  63. { greek code page }
  64. cp1253,
  65. { other code pages }
  66. cp8859_1,cp850,cp437,cp1252,cp646,
  67. cp874, cp856,cp852,cp8859_2,
  68. cp1250,cp1254,cp1255,cp1256,cp1257,cp1258,
  69. {$endif}
  70. globals,cutils;
  71. procedure initwidestring(out r : pcompilerwidestring);
  72. begin
  73. new(r);
  74. r^.data:=nil;
  75. r^.len:=0;
  76. r^.maxlen:=0;
  77. end;
  78. procedure donewidestring(var r : pcompilerwidestring);
  79. begin
  80. if assigned(r^.data) then
  81. freemem(r^.data);
  82. dispose(r);
  83. r:=nil;
  84. end;
  85. function getcharwidestring(r : pcompilerwidestring;l : SizeInt) : tcompilerwidechar;
  86. begin
  87. getcharwidestring:=r^.data[l];
  88. end;
  89. function getlengthwidestring(r : pcompilerwidestring) : SizeInt;
  90. begin
  91. getlengthwidestring:=r^.len;
  92. end;
  93. procedure growwidestring(r : pcompilerwidestring;l : SizeInt);
  94. begin
  95. if r^.maxlen>=l then
  96. exit;
  97. if assigned(r^.data) then
  98. reallocmem(r^.data,sizeof(tcompilerwidechar)*l)
  99. else
  100. getmem(r^.data,sizeof(tcompilerwidechar)*l);
  101. r^.maxlen:=l;
  102. end;
  103. procedure setlengthwidestring(r : pcompilerwidestring;l : SizeInt);
  104. begin
  105. r^.len:=l;
  106. if l>r^.maxlen then
  107. growwidestring(r,l);
  108. end;
  109. procedure concatwidestringchar(r : pcompilerwidestring;c : tcompilerwidechar);
  110. begin
  111. if r^.len>=r^.maxlen then
  112. growwidestring(r,r^.len+16);
  113. r^.data[r^.len]:=c;
  114. inc(r^.len);
  115. end;
  116. procedure concatwidestrings(s1,s2 : pcompilerwidestring);
  117. begin
  118. growwidestring(s1,s1^.len+s2^.len);
  119. move(s2^.data^,s1^.data[s1^.len],s2^.len*sizeof(tcompilerwidechar));
  120. inc(s1^.len,s2^.len);
  121. end;
  122. procedure copywidestring(s,d : pcompilerwidestring);
  123. begin
  124. setlengthwidestring(d,s^.len);
  125. move(s^.data^,d^.data^,s^.len*sizeof(tcompilerwidechar));
  126. end;
  127. function comparewidestrings(s1,s2 : pcompilerwidestring) : SizeInt;
  128. var
  129. maxi,temp : SizeInt;
  130. begin
  131. if pointer(s1)=pointer(s2) then
  132. begin
  133. comparewidestrings:=0;
  134. exit;
  135. end;
  136. maxi:=s1^.len;
  137. temp:=s2^.len;
  138. if maxi>temp then
  139. maxi:=Temp;
  140. temp:=compareword(s1^.data^,s2^.data^,maxi);
  141. if temp=0 then
  142. temp:=s1^.len-s2^.len;
  143. comparewidestrings:=temp;
  144. end;
  145. function asciichar2unicode(c : char) : tcompilerwidechar;
  146. var
  147. m : punicodemap;
  148. begin
  149. if (current_settings.sourcecodepage <> CP_UTF8) then
  150. begin
  151. m:=getmap(current_settings.sourcecodepage);
  152. asciichar2unicode:=getunicode(c,m);
  153. end
  154. else
  155. result:=tcompilerwidechar(c);
  156. end;
  157. function unicode2asciichar(c : tcompilerwidechar) : char;
  158. {begin
  159. if word(c)<128 then
  160. unicode2asciichar:=char(word(c))
  161. else
  162. unicode2asciichar:='?';
  163. end;}
  164. begin
  165. Result := getascii(c,getmap(current_settings.sourcecodepage))[1];
  166. end;
  167. procedure ascii2unicode(p : pchar;l : SizeInt;cp : tstringencoding;r : pcompilerwidestring;codepagetranslation : boolean = true);
  168. var
  169. source : pchar;
  170. dest : tcompilerwidecharptr;
  171. i : SizeInt;
  172. m : punicodemap;
  173. begin
  174. m:=getmap(cp);
  175. setlengthwidestring(r,l);
  176. source:=p;
  177. dest:=tcompilerwidecharptr(r^.data);
  178. if codepagetranslation then
  179. begin
  180. if cp<>CP_UTF8 then
  181. begin
  182. for i:=1 to l do
  183. begin
  184. dest^:=getunicode(source^,m);
  185. inc(dest);
  186. inc(source);
  187. end;
  188. end
  189. else
  190. begin
  191. r^.len:=Utf8ToUnicode(punicodechar(r^.data),r^.maxlen,p,l);
  192. { -1, because utf8tounicode includes room for a terminating 0 in
  193. its result count }
  194. if r^.len>0 then
  195. dec(r^.len);
  196. end;
  197. end
  198. else
  199. begin
  200. for i:=1 to l do
  201. begin
  202. dest^:=tcompilerwidechar(source^);
  203. inc(dest);
  204. inc(source);
  205. end;
  206. end;
  207. end;
  208. procedure unicode2ascii(r : pcompilerwidestring;p:pchar;cp : tstringencoding);
  209. var
  210. m : punicodemap;
  211. source : tcompilerwidecharptr;
  212. dest : pchar;
  213. i : longint;
  214. begin
  215. { can't implement that here, because the memory size for p() cannot
  216. be changed here, and we may need more bytes than have been allocated }
  217. if cp=CP_UTF8 then
  218. internalerrorproc(2015092701);
  219. if (cp = 0) or (cp=CP_NONE) then
  220. m:=getmap(current_settings.sourcecodepage)
  221. else
  222. m:=getmap(cp);
  223. source:=tcompilerwidecharptr(r^.data);
  224. dest:=p;
  225. for i:=1 to r^.len do
  226. begin
  227. dest^ := getascii(source^,m)[1];
  228. inc(dest);
  229. inc(source);
  230. end;
  231. end;
  232. function hasnonasciichars(const p: pcompilerwidestring): boolean;
  233. var
  234. source : tcompilerwidecharptr;
  235. i : longint;
  236. begin
  237. source:=tcompilerwidecharptr(p^.data);
  238. result:=true;
  239. for i:=1 to p^.len do
  240. begin
  241. if word(source^)>=128 then
  242. exit;
  243. inc(source);
  244. end;
  245. result:=false;
  246. end;
  247. function cpavailable(const s: string): boolean;
  248. begin
  249. result:=mappingavailable(lower(s));
  250. {$if FPC_FULLVERSION>20700}
  251. if not result then
  252. result:=(unicodepath<>'')and(registerbinarymapping(unicodepath+'charset',lower(s)));
  253. {$ifend}
  254. end;
  255. function cpavailable(cp: word): boolean;
  256. begin
  257. result:=mappingavailable(cp);
  258. {$if FPC_FULLVERSION>20700}
  259. if not result then
  260. result:=(unicodepath<>'')and(registerbinarymapping(unicodepath+'charset','cp'+tostr(cp)));
  261. {$ifend}
  262. end;
  263. procedure changecodepage(
  264. s : pchar; l : SizeInt; scp : tstringencoding;
  265. d : pchar; dcp : tstringencoding
  266. );
  267. var
  268. ms, md : punicodemap;
  269. source : pchar;
  270. dest : pchar;
  271. i : longint;
  272. begin
  273. ms:=getmap(scp);
  274. md:=getmap(dcp);
  275. source:=s;
  276. dest:=d;
  277. for i:=1 to l do
  278. begin
  279. dest^ := getascii(getunicode(source^,ms),md)[1];
  280. inc(dest);
  281. inc(source);
  282. end;
  283. end;
  284. function codepagebyname(const s : string) : tstringencoding;
  285. var
  286. p : punicodemap;
  287. begin
  288. Result:=0;
  289. p:=getmap(lower(s));
  290. if (p<>nil) then
  291. Result:=p^.cp;
  292. end;
  293. function charlength(p: pchar; len: sizeint): sizeint;
  294. {$IFDEF FPC_HAS_CPSTRING}
  295. var
  296. p2: pchar;
  297. i, chars, codepointlen: sizeint;
  298. {$ENDIF FPC_HAS_CPSTRING}
  299. begin
  300. {$IFDEF FPC_HAS_CPSTRING}
  301. if len=0 then
  302. begin
  303. result:=0;
  304. exit;
  305. end;
  306. { Length of the string converted to a SBCS codepage (e.g. ISO 8859-1)
  307. should be equal to the amount of characters in the source string. }
  308. if defaultsystemcodepage=cp_utf8 then
  309. { ChangeCodePage does not work for UTF-8 apparently... :-( }
  310. begin
  311. i:=1;
  312. chars:=0;
  313. while i<=len do
  314. begin
  315. codepointlen:=utf8codepointlen(p,len-i+1,true);
  316. inc(i,codepointlen);
  317. inc(p,codepointlen);
  318. inc(chars);
  319. end;
  320. result:=chars;
  321. end
  322. else if cpavailable(defaultsystemcodepage) then
  323. begin
  324. getmem(p2,succ(len));
  325. fillchar(p2^,succ(len),0);
  326. changecodepage(p,len,defaultsystemcodepage,p2,28591);
  327. result:=strlen(p2);
  328. freemem(p2,succ(len));
  329. end
  330. else
  331. result:=len;
  332. {$ELSE FPC_HAS_CPSTRING}
  333. result:=len;
  334. {$ENDIF FPC_HAS_CPSTRING}
  335. end;
  336. function charlength(const s: string): sizeint;
  337. begin
  338. result:=charlength(@s[1],length(s));
  339. end;
  340. end.