Browse Source

+ implemented StrWidth and CStrLen, using the new extended grapheme cluster string enumerator

git-svn-id: branches/unicodekvm@48729 -
nickysn 4 years ago
parent
commit
e583ba6cfa
2 changed files with 13 additions and 11 deletions
  1. 1 1
      packages/fv/examples/testuapp.lpi
  2. 12 10
      packages/fv/src/drivers.inc

+ 1 - 1
packages/fv/examples/testuapp.lpi

@@ -39,7 +39,7 @@
     </Target>
     <SearchPaths>
       <IncludeFiles Value="$(ProjOutDir);../../rtl-console/src/inc"/>
-      <OtherUnitFiles Value="../src;../../rtl-console/src/unix;../../rtl-extra/src/inc"/>
+      <OtherUnitFiles Value="../src;../../rtl-console/src/unix;../../rtl-extra/src/inc;../../rtl-unicode/src/inc"/>
       <UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)"/>
     </SearchPaths>
   </CompilerOptions>

+ 12 - 10
packages/fv/src/drivers.inc

@@ -110,6 +110,7 @@ USES
    SysMsg,
 {$ifdef FV_UNICODE}
    UFVCommon,
+   GraphemeBreakProperty,
 {$else FV_UNICODE}
    FVCommon,
 {$endif FV_UNICODE}
@@ -940,10 +941,12 @@ end;
 
 {$ifdef FV_UNICODE}
 FUNCTION StrWidth(Const S: Sw_String): Sw_Integer;
+VAR EGC: Sw_String;
 BEGIN
-   { todo: split string into extended grapheme clusters properly, handle non-BMP characters,
-     handle wide (CJK) characters, etc. }
-   StrWidth := Length(S);
+   { todo: handle wide (CJK, emoji) characters as double width }
+   Result := 0;
+   for EGC in TUnicodeStringExtendedGraphemeClustersEnumerator.Create(S) do
+     Inc(Result);
 END;
 {$else FV_UNICODE}
 FUNCTION StrWidth(Const S: Sw_String): Sw_Integer;
@@ -957,14 +960,13 @@ END;
 {---------------------------------------------------------------------------}
 {$ifdef FV_UNICODE}
 FUNCTION CStrLen (Const S: UnicodeString): Sw_Integer;
-VAR I, J: Sw_Integer;
+VAR EGC: Sw_String;
 BEGIN
-   { todo: split string into extended grapheme clusters properly, handle non-BMP characters,
-     handle wide (CJK) characters, etc. }
-   J := 0;                                            { Set result to zero }
-   For I := 1 To Length(S) Do
-     If (S[I] <> '~') Then Inc(J);                    { Inc count if not ~ }
-   CStrLen := J;                                      { Return length }
+   { todo: handle wide (CJK, emoji) characters as double width }
+   Result := 0;
+   for EGC in TUnicodeStringExtendedGraphemeClustersEnumerator.Create(S) do
+     if EGC <> '~' then
+       Inc(Result);
 END;
 {$else FV_UNICODE}
 FUNCTION CStrLen (Const S: String): Sw_Integer;