2
0

jsstrings.inc 10 KB

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