tissurrogatepair2.pp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. program tissurrogatepair2;
  2. {$ifdef FPC}
  3. {$mode objfpc}
  4. {$H+}
  5. {$PACKENUM 1}
  6. {$endif fpc}
  7. {$ifndef FPC}
  8. {$APPTYPE CONSOLE}
  9. {$endif}
  10. uses
  11. SysUtils,
  12. unicodedata,character;
  13. {$ifndef FPC}
  14. type UnicodeChar = WideChar;
  15. {$endif}
  16. const
  17. { test only a spare grid, else the test runs too long (testing all combinations means dist=1) }
  18. dist = 8;
  19. procedure DoError(ACode : Integer; ACodePoint1, ACodePoint2 : Integer); overload;
  20. begin
  21. WriteLn(
  22. 'Error #',ACode,
  23. ' ; CodePoint1 = ',IntToHex(ACodePoint1,4),
  24. ' ; CodePoint2 = ',IntToHex(ACodePoint2,4)
  25. );
  26. Halt(Acode);
  27. end;
  28. var
  29. e, i , j: Integer;
  30. count : int64;
  31. s : UnicodeString;
  32. begin
  33. s := 'azerty12345';
  34. e := 1;
  35. count:=0;
  36. for i := HIGH_SURROGATE_BEGIN to HIGH_SURROGATE_END do begin
  37. for j := LOW_SURROGATE_BEGIN to LOW_SURROGATE_END do begin
  38. s[3] := UnicodeChar(i);
  39. s[4] := UnicodeChar(j);
  40. if not TCharacter.IsSurrogatePair(s,3) then
  41. DoError(e,i,j);
  42. inc(count);
  43. end;
  44. end;
  45. Inc(e);
  46. for i := Low(Word) to High(Word) div dist do begin
  47. if (i*dist < HIGH_SURROGATE_BEGIN) or (i*dist > HIGH_SURROGATE_END) then begin
  48. for j := Low(Word) to High(Word) div dist do begin
  49. if (j*dist < LOW_SURROGATE_BEGIN) or (j*dist > LOW_SURROGATE_END) then begin
  50. s[5] := UnicodeChar(i*dist);
  51. s[6] := UnicodeChar(j*dist);
  52. if TCharacter.IsSurrogatePair(s,5) then
  53. DoError(e,i*dist,j*dist);
  54. inc(count);
  55. end;
  56. end;
  57. end;
  58. end;
  59. WriteLn('ok, count=',count);
  60. end.