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