jsstrings.inc 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2005, 2011 by Florian Klaempfl, Jonas Maebe
  4. members of the Free Pascal development team.
  5. This file implements support routines for Shortstrings with FPC/JVM
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. constructor ShortstringClass.Create(const arr: array of ansichar; maxlen: byte);
  13. begin
  14. setlength(fdata,maxlen);
  15. if high(arr)=-1 then
  16. exit;
  17. curlen:=min(high(arr)+1,maxlen);
  18. JLSystem.ArrayCopy(JLObject(@arr),0,JLObject(fdata),0,curlen);
  19. end;
  20. constructor ShortstringClass.Create(const arr: array of unicodechar; maxlen: byte);
  21. begin
  22. if high(arr)=-1 then
  23. begin
  24. setlength(fdata,maxlen);
  25. exit;
  26. end;
  27. fdata:=TAnsiCharArray(JLString.Create(arr).getBytes);
  28. setlength(fdata,maxlen);
  29. curlen:=min(high(fdata)+1,maxlen);
  30. end;
  31. constructor ShortstringClass.Create(const u: unicodestring; maxlen: byte);
  32. begin
  33. if system.length(u)=0 then
  34. begin
  35. setlength(fdata,maxlen);
  36. exit;
  37. end;
  38. fdata:=TAnsiCharArray(JLString(u).getBytes);
  39. setlength(fdata,maxlen);
  40. curlen:=min(high(fdata)+1,maxlen);
  41. end;
  42. constructor ShortstringClass.Create(const a: ansistring; maxlen: byte);
  43. var
  44. alen: jint;
  45. begin
  46. setlength(fdata,maxlen);
  47. alen:=system.length(a);
  48. if alen=0 then
  49. exit;
  50. curlen:=min(alen,maxlen);
  51. JLSystem.ArrayCopy(JLObject(AnsistringClass(a).fdata),0,JLObject(fdata),0,curlen);
  52. end;
  53. constructor ShortstringClass.Create(const s: shortstring; maxlen: byte);overload;
  54. begin
  55. setlength(fdata,maxlen);
  56. if system.length(s)=0 then
  57. exit;
  58. curlen:=min(system.length(s),maxlen);
  59. JLSystem.ArrayCopy(JLObject(ShortstringClass(@s).fdata),0,JLObject(fdata),0,min(system.length(s),maxlen));
  60. end;
  61. constructor ShortstringClass.Create(ch: ansichar; maxlen: byte);overload;
  62. begin
  63. setlength(fdata,maxlen);
  64. fdata[0]:=ch;
  65. curlen:=1;
  66. end;
  67. constructor ShortstringClass.Create(ch: unicodechar; maxlen: byte);overload;
  68. begin
  69. fdata:=TAnsiCharArray(JLString.Create(ch).getBytes);
  70. curlen:=min(system.length(fdata),maxlen);
  71. setlength(fdata,maxlen);
  72. end;
  73. class function ShortstringClass.CreateEmpty(maxlen: byte): ShortstringClass;
  74. begin
  75. result:=ShortstringClass.Create;
  76. setlength(result.fdata,maxlen);
  77. end;
  78. class function ShortstringClass.CreateFromLiteralStringBytes(const u: unicodestring): shortstring;
  79. var
  80. i: longint;
  81. begin
  82. { used to construct constant shortstrings from Java string constants }
  83. ShortstringClass(@result).curlen:=min(system.length(u),255);
  84. setlength(ShortstringClass(@result).fdata,ShortstringClass(@result).curlen);
  85. for i:=1 to ShortstringClass(@result).curlen do
  86. ShortstringClass(@result).fdata[i-1]:=ansichar(ord(u[i]));
  87. end;
  88. procedure ShortstringClass.FpcDeepCopy(dest: ShortstringClass);
  89. var
  90. destmaxlen,
  91. copylen: longint;
  92. begin
  93. dest.curlen:=curlen;
  94. copylen:=system.length(fdata);
  95. destmaxlen:=system.length(dest.fdata);
  96. if copylen>destmaxlen then
  97. begin
  98. copylen:=destmaxlen;
  99. dest.curlen:=destmaxlen;
  100. end;
  101. if copylen>0 then
  102. JLSystem.ArrayCopy(JLObject(fdata),0,JLObject(dest.fdata),0,copylen);
  103. end;
  104. procedure ShortstringClass.setChar(index: jint; char: ansichar);
  105. begin
  106. { index is 1-based here }
  107. { support accessing the length byte }
  108. if index=0 then
  109. curlen:=ord(char)
  110. else
  111. fdata[index-1]:=char;
  112. end;
  113. function ShortstringClass.charAt(index: jint): ansichar;
  114. begin
  115. { index is already decreased by one, because same calling code is used for
  116. JLString.charAt() }
  117. { support accessing the length byte }
  118. if (index=-1) then
  119. result:=ansichar(curlen)
  120. else
  121. result:=fdata[index];
  122. end;
  123. function ShortstringClass.toUnicodeString: unicodestring;
  124. begin
  125. result:=UnicodeString(toString);
  126. end;
  127. function ShortstringClass.toAnsistring: ansistring;
  128. begin
  129. result:=ansistring(AnsistringClass.Create(pshortstring(self)^));
  130. end;
  131. function ShortstringClass.toString: JLString;
  132. begin
  133. if curlen<>0 then
  134. result:=JLString.Create(TJByteArray(fdata),0,curlen)
  135. else
  136. result:='';
  137. end;
  138. function ShortstringClass.clone: JLObject;
  139. begin
  140. result:=ShortstringClass.Create(pshortstring(self)^,system.length(fdata));
  141. end;
  142. function ShortstringClass.length: jint;
  143. begin
  144. result:=curlen;
  145. end;
  146. class function AnsiCharArrayClass.CreateFromLiteralStringBytes(const u: unicodestring): TAnsiCharArray;
  147. var
  148. i: longint;
  149. begin
  150. { used to construct constant chararrays from Java string constants }
  151. setlength(result,length(u)+1);
  152. for i:=1 to length(u) do
  153. result[i-1]:=ansichar(ord(u[i]));
  154. result[length(u)]:=#0;
  155. end;
  156. {$define FPC_HAS_SHORTSTR_SHORTSTR_INTERN_CHARMOVE}
  157. procedure fpc_shortstr_shortstr_intern_charmove(const src: shortstring; const srcindex: byte; var dst: shortstring; const dstindex, len: byte); {$ifdef SYSTEMINLINE}inline;{$endif}
  158. begin
  159. JLSystem.arraycopy(JLObject(ShortstringClass(@src).fdata),srcindex-1,JLObject(ShortstringClass(@dst).fdata),dstindex-1,len);
  160. end;
  161. {$define FPC_HAS_SHORTSTR_CHARARRAY_INTERN_CHARMOVE}
  162. procedure fpc_shortstr_chararray_intern_charmove(const src: shortstring; out dst: array of char; const len: sizeint); {$ifdef SYSTEMINLINE}inline;{$endif}
  163. begin
  164. JLSystem.arraycopy(JLObject(ShortstringClass(@src).fdata),0,JLObject(@dst),0,len);
  165. end;
  166. {$define FPC_HAS_CHAR_TO_SHORTSTR}
  167. procedure fpc_Char_To_ShortStr(out res : shortstring;const c : AnsiChar) compilerproc;
  168. {
  169. Converts an AnsiChar to a ShortString;
  170. }
  171. begin
  172. setlength(res,1);
  173. ShortstringClass(@res).fdata[0]:=c;
  174. end;
  175. {$define FPC_HAS_SHORTSTR_POS_SHORTSTR}
  176. Function Pos (Const Substr : Shortstring; Const s : Shortstring) : SizeInt;
  177. var
  178. i,j,k,MaxLen, SubstrLen : SizeInt;
  179. begin
  180. Pos:=0;
  181. SubstrLen:=Length(SubStr);
  182. if SubstrLen>0 then
  183. begin
  184. MaxLen:=Length(s)-Length(SubStr);
  185. i:=0;
  186. while (i<=MaxLen) do
  187. begin
  188. inc(i);
  189. j:=0;
  190. k:=i-1;
  191. while (j<SubstrLen) and
  192. (ShortstringClass(@SubStr).fdata[j]=ShortstringClass(@s).fdata[k]) do
  193. begin
  194. inc(j);
  195. inc(k);
  196. end;
  197. if (j=SubstrLen) then
  198. begin
  199. Pos:=i;
  200. exit;
  201. end;
  202. end;
  203. end;
  204. end;
  205. {$define FPC_HAS_SHORTSTR_POS_CHAR}
  206. {Faster when looking for a single char...}
  207. function pos(c:char;const s:shortstring):SizeInt;
  208. var
  209. i : SizeInt;
  210. begin
  211. for i:=0 to length(s)-1 do
  212. begin
  213. if ShortStringClass(@s).fdata[i]=c then
  214. begin
  215. pos:=i+1;
  216. exit;
  217. end;
  218. end;
  219. pos:=0;
  220. end;
  221. {$define FPC_UPCASE_SHORTSTR}
  222. function upcase(const s : shortstring) : shortstring;
  223. var
  224. u : unicodestring;
  225. begin
  226. u:=s;
  227. result:=upcase(u);
  228. end;
  229. {$define FPC_UPCASE_CHAR}
  230. Function upCase(c:Char):Char;
  231. var
  232. u : unicodestring;
  233. s: ansistring;
  234. begin
  235. u:=c;
  236. s:=upcase(u);
  237. c:=s[1];
  238. end;
  239. {$define FPC_LOWERCASE_SHORTSTR}
  240. function lowercase(const s : shortstring) : shortstring;
  241. var
  242. u : unicodestring;
  243. begin
  244. u:=s;
  245. result:=lowercase(u);
  246. end;
  247. {$define FPC_LOWERCASE_CHAR}
  248. Function lowerCase(c:Char):Char; overload;
  249. var
  250. u : unicodestring;
  251. s: ansistring;
  252. begin
  253. u:=c;
  254. s:=lowercase(u);
  255. c:=s[1];
  256. end;
  257. { defined as external aliases to the int64 versions }
  258. {$define FPC_HAS_QWORD_OCT_SHORTSTR}
  259. {$define FPC_HAS_QWORD_BIN_SHORTSTR}
  260. {$define FPC_HAS_QWORD_HEX_SHORTSTR}
  261. {$define FPC_HAS_HEXSTR_POINTER_SHORTSTR}
  262. function hexstr(val : pointer) : shortstring;
  263. begin
  264. hexstr:=hexstr(JLObject(val).hashCode,sizeof(pointer)*2);
  265. end;
  266. {$define FPC_HAS_SPACE_SHORTSTR}
  267. function space (b : byte): shortstring;
  268. begin
  269. setlength(result,b);
  270. if b>0 then
  271. JUArrays.fill(TJByteArray(ShortstringClass(@result).fdata),0,b,ord(' '))
  272. end;
  273. {*****************************************************************************
  274. Str() Helpers
  275. *****************************************************************************}
  276. { this is a bit of a hack: 'public name' aliases don't work yet on the JVM
  277. target, so manually add the redirection from FPC_VAL_SINT_SHORTSTR
  278. to the fpc_Val_SInt_ShortStr compilerproc since compilerprocs are all lower
  279. case }
  280. {$ifndef FPC_HAS_INT_VAL_SINT_SHORTSTR}
  281. {$define FPC_HAS_INT_VAL_SINT_SHORTSTR}
  282. { we need this for fpc_Val_SInt_Ansistr and fpc_Val_SInt_WideStr because }
  283. { we have to pass the DestSize parameter on (JM) }
  284. Function int_Val_SInt_ShortStr(DestSize: SizeInt; Const S: ShortString; out Code: ValSInt): ValSInt; [external name 'fpc_val_sint_shortstr'];
  285. {$endif FPC_HAS_INT_VAL_SINT_SHORTSTR}
  286. {$define FPC_HAS_SETSTRING_SHORTSTR}
  287. Procedure SetString (Out S : Shortstring; Buf : PChar; Len : SizeInt);
  288. begin
  289. If Len > High(S) then
  290. Len := High(S);
  291. SetLength(S,Len);
  292. If Buf<>Nil then
  293. begin
  294. JLSystem.arraycopy(JLObject(Buf),0,JLObject(ShortstringClass(@S).fdata),0,len);
  295. end;
  296. end;
  297. {$define FPC_HAS_COMPARETEXT_SHORTSTR}
  298. function ShortCompareText(const S1, S2: shortstring): SizeInt;
  299. var
  300. c1, c2: Byte;
  301. i: Integer;
  302. L1, L2, Count: SizeInt;
  303. P1, P2: PChar;
  304. begin
  305. L1 := Length(S1);
  306. L2 := Length(S2);
  307. if L1 > L2 then
  308. Count := L2
  309. else
  310. Count := L1;
  311. i := 0;
  312. P1 := @ShortstringClass(@S1).fdata[0];
  313. P2 := @ShortstringClass(@S2).fdata[0];
  314. c1 := 0;
  315. c2 := 0;
  316. while i < count do
  317. begin
  318. c1 := byte(p1[i]);
  319. c2 := byte(p2[i]);
  320. if c1 <> c2 then
  321. begin
  322. if c1 in [97..122] then
  323. Dec(c1, 32);
  324. if c2 in [97..122] then
  325. Dec(c2, 32);
  326. if c1 <> c2 then
  327. Break;
  328. end;
  329. Inc(I);
  330. end;
  331. if i < count then
  332. ShortCompareText := c1 - c2
  333. else
  334. ShortCompareText := L1 - L2;
  335. end;
  336. { not based on Delphi-style rtti }
  337. {$define FPC_STR_ENUM_INTERN}
  338. function fpc_shortstr_enum_intern(enum: JLEnum; len:sizeint;out s:shortstring): longint;
  339. begin
  340. s:=enum.toString;
  341. if length(s)<len then
  342. s:=space(len-length(s))+s;
  343. end;