summaryrefslogtreecommitdiff
path: root/gfx/schar16.pas
blob: 156004bab176555c4a37d93c524f6aa3a6ad8aee (plain)
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
{ schar16.pas: Function for handling 16 bit unicode in normal 8 bit ansi strings
  File maintainer: nvitya@freemail.hu

History:
}

unit schar16;

{$ifdef FPC}
{$mode objfpc}{$H+}
{$endif}

interface

uses
  Classes, SysUtils;

const
  u8escchar  : char = '^';
  u8escclose : char = ';';

type
  String16 = string;
  String8  = string;

  Char16 = packed record
             char1 : char;
             char2 : char;
           end;

function Str8to16(s : String8) : string16; // = u8
function Str16to8(s : String16) : string8;  // = u16u8

function u8(s : String8) : string16;       // decodes escaped text
function u8u16(s : String8) : string16;    // same as u8
function u8noesc(s : String8) : string16;  // inserts #0-s, doesn't use escapes

function u16u8(s : string16) : string8;    // escapes only 256..65535
function u16u8safe(s : string16) : string8; // escapes 0.31 and 255..65535
function u16u8trunc(s : string16) : string8; // truncates hi byte, doesn't use escapes ('^' -> '^'), try handle code pages
// this function could cause information loss

function Length16(s : String16) : integer;
procedure SetLength16(var s : String16; len : integer);

function Pos16(const s : string16; const searched : string16) : integer;
function UpCase16(const s : string16) : string16;
function Upper16(const s : string16) : string16;

function Copy16(s : String16; ind,len : integer) : String16;
procedure Insert16(s : String16; var dest : string16; ind : integer);
procedure Delete16(var s : String16; ind, count : integer);

procedure AddChar16(var s : String16; ch16 : Char16); overload;
procedure AddChar16(var s : String16; w : word); overload;

implementation

uses UnitKeys;

function u8(s : String8) : string16;
var
  n : integer;
  ccode : word;
  c : char;
begin
  // 'asdf^123^452^354;78;
  result := '';
  n := 1;
  while (n <= length(s)) do
  begin
    if s[n] = u8escchar then
    begin
      inc(n);
      if (n <= length(s)) and (s[n] = u8escchar) then
      begin
        result := result + u8escchar + #0;
        inc(n);
        continue;
      end;

      ccode := 0;
      while (n <= length(s)) do
      begin
        c := s[n];
        if (c >= '0') and (c <= '9') then ccode := ccode * 10 + (ord(c)-ord('0'))
        else
            break;
        inc(n);
      end;
      result := result + chr(lo(ccode)) + chr(hi(ccode));

      if (n <= length(s)) and (s[n] = u8escclose) then inc(n);
    end
    else
    begin
      result := result + s[n] + #0;
      inc(n);
    end;
  end;
end;

function u8u16(s : String8) : string16;    // same as u8
begin
  result := u8(s);
end;

function u16u8safe(s : string16) : string8;
var
  ccode : word;
  n : integer;
  uni : boolean;
begin
  result := '';
  uni := false;
  n := 1;
  while n < length(s) do
  begin
    ccode := ord(s[n]) + (ord(s[n+1]) shl 8);
    if (ccode < 32) or (ccode > 254) then
    begin
       result := result + u8escchar + IntToStr(ccode);
       uni := true;
    end
    else
    begin
      if ccode = ord(u8escchar) then result := result + u8escchar + u8escchar
      else
      begin
        if uni and (ccode >= ord('0')) and (ccode <= ord('9')) then result := result + u8escclose;
        result := result + chr(ccode);
      end;
      uni := false;
    end;
    inc(n,2);
  end;
end;

function u16u8(s : string16) : string8;
var
  ccode : word;
  n : integer;
  uni : boolean;
begin
  result := '';
  uni := false;
  n := 1;
  while n < length(s) do
  begin
    ccode := ord(s[n]) + (ord(s[n+1]) shl 8);
    if (ccode > 255) then
    begin
       result := result + u8escchar + IntToStr(ccode);
       uni := true;
    end
    else
    begin
      if ccode = ord(u8escchar) then result := result + u8escchar + u8escchar
      else
      begin
        if uni and (ccode >= ord('0')) and (ccode <= ord('9')) then result := result + u8escclose;
        result := result + chr(ccode);
      end;
      uni := false;
    end;
    inc(n,2);
  end;
end;

function u16u8trunc(s : string16) : string8;
var
  n : integer;
  i : integer;
  len : integer;
  ct,c : char;
begin
  SetLength(Result,length16(s));
  i := 1;
  len := length16(s);
  for n := 1 to len do
  begin
    ct := s[i+1];
    c  := s[i];
    // some hungarian translation:
    if ct = #1 then
    begin
      c := TranslateChar(ct,c);
    end;
    Result[n] := c;
    inc(i,2);
  end;
end;

function u8noesc(s: String8): string16;
var
  n : integer;
  i : integer;
  len : integer;
begin
  SetLength16(Result,length(s));
  i := 1;
  len := length(s);
  for n := 1 to len do
  begin
    Result[i] :=  s[n];
    inc(i);
    Result[i] := #0;
    inc(i);
  end;
end;

function Str8to16(s : String8) : string16;
begin
  result := u8(s);
end;

function Str16to8(s : String16) : string8;
begin
  result := u16u8(s);
end;

{
function Str8to16(s : String8) : string16;
var
  n : integer;
  i : integer;
  len : integer;
begin
  SetLength16(Result,length(s));
  i := 1;
  len := length(s);
  for n := 1 to len do
  begin
    Result[i] :=  s[n];
    inc(i);
    Result[i] := #0;
    inc(i);
  end;
end;

function Str16to8(s : String8) : string8;
var
  n : integer;
  i : integer;
  len : integer;
  ct,c : char;
begin
  SetLength(Result,length16(s));
  i := 1;
  len := length16(s);
  for n := 1 to len do
  begin
    ct := s[i+1];
    c  := s[i];
    // some hungarian translation:
    if ct = #1 then
    begin
      c := TranslateChar(ct,c);
    end;
    Result[n] := c;
    inc(i,2);
  end;
end;
}

procedure SetLength16(var s : String16; len : integer);
begin
  if len >= 0 then SetLength(s,len shl 1);
end;

function Length16(s : String16) : integer;
begin
  Result := Length(s) shr 1;
end;

function Pos16(const s : string16; const searched : string16) : integer;
var
  n: integer;
begin
  result := 0;
  if length16(s) < 1 then Exit;
  for n := 1 to Length16(searched)-Length16(s) do
  begin
    if CompareMem(@s[1], @searched[n*2-1], Length16(s)) then
    begin
      result := n;
      Exit;
    end;
  end;
end;

function UpCase16(const s : string16) : string16;
begin
  if length(s) < 2 then Exit;
  if s[2] = #0 then Result := UpCase(s[1])+#0
  else
  begin
    result := chr(ord(s[1]) and $FE) + s[2];
  end;
end;

function Upper16(const s : string16) : string16;
var
  n : integer;
begin
  result := '';
  for n := 1 to length16(s) do
  begin
    result := result + UpCase16(s[n*2-1]+s[n*2]);
  end;
end;

function Copy16(s : String16; ind,len : integer) : String16;
begin
  result := copy(s,1 + ((ind-1) shl 1), len shl 1);
end;

procedure Insert16(s : String16; var dest : string16; ind : integer);
begin
  Insert(s,dest,1 + ((ind-1) shl 1));
end;

procedure Delete16(var s : String16; ind, count : integer);
begin
  Delete(s,1 + ((ind-1) shl 1), count shl 1);
end;

procedure AddChar16(var s : String16; ch16 : Char16);
begin
  s := s + ch16.char1 + ch16.char2;
end;

procedure AddChar16(var s : String16; w : word);
begin
  s := s + chr(lo(w)) + chr(hi(w));
end;

end.