Hi everyone ;
I need help to convert a pascal code(or some of its functions) to lua, the code help link Arabic glyphs,reverse them and execlude numbers(as Arabic is bidirectional).
//FUNCTION TO REPLACE PATTERNS OCCURNCE Function WideStringReplace(const S, OldPattern, NewPattern: WideString; Flags: TReplaceFlags): WideString; var Srch,OldP,RemS: WideString; // Srch and Oldp can contain uppercase versions of S,OldPattern P : Integer; begin Srch:=S; OldP:=OldPattern; if rfIgnoreCase in Flags then begin Srch:=WideUpperCase(Srch); OldP:=WideUpperCase(OldP); end; RemS:=S; Result:=''; while (Length(Srch)\<\>0) do begin P:=Pos(OldP, Srch); if P=0 then begin Result:=Result+RemS; Srch:=''; end else begin Result:=Result+Copy(RemS,1,P-1)+NewPattern; P:=P+Length(OldP); RemS:=Copy(RemS,P,Length(RemS)-P+1); if not (rfReplaceAll in Flags) then begin Result:=Result+RemS; Srch:=''; end else Srch:=Copy(Srch,P,Length(Srch)-P+1); end; end; end; //REVERSE LETTERS AND EXECLUDE NUMBERS function reverse( atext:widestring):widestring; var i:integer; wdcr1,wdcr2,text:widestring; begin for I :=0 to Length(Atext)-1 do begin if I \< Length(Atext) then begin wdcr1 :=Atext[I+1]; if ((wdcr1 \>= #$0030) and (wdcr1 \<= #$0039 )) or ((wdcr1 \>= #$0660) and (wdcr1 \<= #$0669)) then begin text:=wdcr1+text; Insert( wdcr1,result,length(text)); wdcr1:=#0; end else begin Insert( wdcr1,result,1); wdcr1:=#0; text:=''; end; end ; end; end; /////////////////////////////////////////////// //A TABLE OF ARABIC GLYPHS ACCORDING TO UNICODE WITH DIFFRENT GLYPH SHAPE FOR EACH LETTER ACCORDING TO ITS POSITION type TGlyphRecord = record Isolated, Final, Initial, Medial: WideChar; end; const GlyphTable: array[#$0621..#$0651] of TGlyphRecord = ( (Isolated:#$fe80), (Isolated:#$fe81; Final:#$fe82), (Isolated:#$fe83; Final:#$fe84), (Isolated:#$fe85;final:#$fe86), (Isolated:#$FE87;Final:#$FE88), //hemza teht ya (isolated:#$FE89;Final:#$fe8a; Initial:#$fe8b; Medial:#$fe8c), (Isolated:#$fe8d; Final:#$fe8e), // alef (isolated:#$FE8F;Final:#$fe90; Initial:#$fe91; Medial:#$fe92), //ba repeated (Isolated:#$fe93;Final:#$fe94), //ta marbootah (Isolated:#$fe95; Final:#$fe96; Initial:#$fe97; Medial:#$fe98), //ta (Isolated:#$fe99;Final:#$fe9a; Initial:#$fe9b; Medial:#$fe9c), //tha (Isolated:#$fe9d;Final:#$fe9e; Initial:#$fe9f; Medial:#$FEA0), //jeem (Isolated:#$fea1; Final:#$fea2; Initial:#$fea3; Medial:#$FEA4), // ha (Isolated:#$fea5; Final:#$fea6; Initial:#$fea7; Medial:#$FEA8), //kha (Isolated:#$fea9;Final:#$feaa), //dal (Isolated:#$feab;Final:#$feac), //dhal (Isolated:#$fead;Final:#$feae;), // Initial:#$feae; Medial:#$feae //ra (Isolated:#$feaf;Final:#$feb0), //za (Isolated:#$feb1;Final:#$feb2; Initial:#$feb3; Medial:#$feb4 ), //seen (Isolated:#$feb5; Final:#$feb6; Initial:#$feb7; Medial:#$feb8), //sheen (Isolated:#$feb9; Final:#$feba; Initial:#$febb; Medial:#$febc), //ssad (Isolated:#$febd; Final:#$febe; Initial:#$febf; Medial:#$fec0), //dhad (Isolated:#$fec1;Final:#$fec2; Initial:#$fec3; Medial:#$fec4), //tda (Isolated:#$fec5;Final:#$fec6; Initial:#$fec7; Medial:#$fec8), //thda (Isolated:#$fec9; Final:#$feca; Initial:#$fecb; Medial:#$fecc), //ain (Isolated:#$fecd; Final:#$fece; Initial:#$fecf; Medial:#$fed0), //ghain (), (), (), (), (), (), (Isolated:#$fed1; Final:#$fed2; Initial:#$fed3; Medial:#$fed4), //fa (Isolated:#$fed5; Final:#$fed6; Initial:#$fed7; Medial:#$fed8), //gaf (Isolated:#$fed9; Final:#$feda; Initial:#$fedb; Medial:#$fedc), //kaf (Isolated:#$fedd;final:#$fede; Initial:#$fedf; Medial:#$fee0), //lam (Isolated:#$fee1;final:#$fee2; Initial:#$fee3;medial:#$Fee4), //meem (Isolated:#$fee5;final:#$fee6; Initial:#$fee7;medial:#$fee8), //noon //noon (Isolated:#$fee9;final:#$feea; Initial:#$feeb; Medial:#$feec), //heh (Isolated:#$feed;Final:#$feee), //wow (Isolated:#$feef; Final:#$fef0), //alef mongaleba ya (Isolated:#$fef1; Final:#$fef2; Initial:#$fef3;Medial:#$fef4), //ya (Isolated:#$FEFB;Final:#$FEFc;medial:#$FEFC), (Isolated:#$061F), (), (), (), (), (Medial:#$fe7d) ); //FUNCTION FOR CHOOSING FROM THE ABOVE UNICODE TABLE DUE TO LAST POSITION OCCURMENT function wrt(text:widestring): WideString; const Alflam: array[1..8] of widestring= (#$fedf#$FE8e, #$fee0#$FE8e, #$fedf#$FE84, #$fee0#$FE84,#$fedf#$FE88, #$fee0#$FE88, #$fedf#$FE82,#$fee0#$FE82) ; const Alf: array[1..8] of widechar= (#$fefb,#$fefc,#$fef7,#$fef8, #$fef9,#$fefa , #$fef5,#$fef6); var c,I: Integer; change, LinkBefore, LinkAfter: Boolean; Current, Next: WideChar; Glyph: TGlyphRecord; cn1:integer; BEGIN LinkBefore := False; //////////////////// for I :=1 to Length(Text) do begin Current := Text[I]; if I \< Length(Text) then Next := Text[I + 1] else Next := #0; change:=(current=#$0623)and(next=#$0644); LinkAfter := (Next \> #$0620) and (Next \<= #$0651) and ((GlyphTable[Next].Final \<\> #0) or (GlyphTable[Next].Medial \<\> #0)); if (Current \>= #$0621) and (Current \<= #$0651) then begin Glyph := GlyphTable[Current]; if LinkBefore and LinkAfter and (Glyph.Medial \<\> #0) then Text[I] := Glyph.Medial else if LinkAfter and (Glyph.Initial \<\> #0) then Text[I] := Glyph.Initial else if LinkBefore and (Glyph.Final \<\> #0) then Text[I] := Glyph.Final else if Glyph.Isolated \<\> #0 then Text[I] := Glyph.Isolated; LinkBefore := ((Glyph.Initial \<\> #0) or (Glyph.Medial \<\> #0)) and LinkAfter; end else LinkBefore := False; // cn2:=0; end; for cn1:=1 to high(alf) do begin text:= WideStringReplace( text,alflam[cn1],alf[cn1],[rfReplaceAll, rfIgnoreCase] ); end; result:=reverse(text); exit; end; Function Editarabic(text:string):string; Begin if lang=1 then result:=text; if lang=2 then result:=utf8encode(widestring(wrt(utf8decode(text))) ); exit; end; end.