Recent

Author Topic: Where is the error in this function?  (Read 8912 times)

anna

  • Sr. Member
  • ****
  • Posts: 426
Re: Where is the error in this function?
« Reply #15 on: March 11, 2018, 04:37:01 pm »
Try this:

Code: [Select]
function RemoveDiacritics(S :utf8string):utf8string;
// It should work for several languages
// Portuguese, Spanish and Italian, maybe for French, German and more
const
  AccentedChars :array[0..53] of utf8string = ('á','à','ã','â','ä','é','è','ê','ë','í','ì','ï','î','ó','ò','õ','ô','ö','ø','ú','ù','ü','û','ç','ñ','ÿ','ý',
                                               'Á','À','Ã','Â','Ä','É','È','Ê','Ë','Í','Ì','Ï','Î','Ó','Ò','Õ','Ô','Ö','Ø','Ú','Ù','Ü','Û','Ç','Ñ','Y','Ý');
  NormalChars   :array[0..53] of utf8string = ('a','a','a','a','a','e','e','e','e','i','i','i','i','o','o','o','o','oe','o','u','u','u','u','c','n','y','y',
                                               'A','A','A','A','A','E','E','E','E','I','I','I','I','O','O','O','O','OE','O','U','U','U','U','C','N','Y','Y');
var
  i, j :integer;
begin
  Result := S;
  for i := 0 to High(AccentedChars) do
    Result := StringReplace(Result, AccentedChars[i], NormalChars[i], [rfReplaceAll]);
end; 

Hello. This code doesn't work . I have used a project from this message http://forum.lazarus.freepascal.org/index.php/topic,15576.msg83821.html#msg83821


I have attached images showing how arrays look like in memory. I think the problem is that the characters  with diacritics in AccentedChars are written as 4 bytes. While the string from the edit contains 2 bytes.

Suggestion from here  helps, making first array consisting of 2-byte chars (each array element occupies  16 bytes ).
« Last Edit: March 11, 2018, 05:24:51 pm by anna »
WinXP SP3 Pro Russian 32-bit (5.1.2600)

 

TinyPortal © 2005-2018