@@ -7,19 +7,35 @@ interface
77
88uses
99 FMX.Edit, FMX.Text, FMX.StdCtrls, FMX.Objects,
10- System.Classes, System.MaskUtils, System.DateUtils, System.Math,
11- System.SysUtils, System.SysConst, System.TypInfo, System.StrUtils;
10+ Classes, MaskUtils, DateUtils, Math, SysUtils, SysConst, TypInfo, StrUtils;
11+
12+ const
13+ csNumbers = [' 0' .. ' 9' ];
14+ csIntegers = [' 0' .. ' 9' , ' -' ];
15+ csCharacters = [' a' .. ' z' , ' A' .. ' Z' ];
16+ csCurrencyDigits = [' 0' .. ' 1' , ' -' , ' ,' ];
17+ csFormatIdentifier = [' #' , ' L' , ' l' , ' 9' ];
18+ csSymbols = [' \' , ' /' , ' -' , ' =' , ' +' , ' *' , ' ,' , ' .' , ' ;' , ' :' , ' |' , ' [' , ' ]' , ' {' , ' }' ,
19+ ' (' , ' )' , ' $' , ' %' , ' @' , ' #' , ' &' , ' !' , ' ?' , ' ª' , ' º' , ' °' , ' ₢' , ' £' , ' ¢' , ' ¬' ,
20+ ' ¨' , ' §' ];
21+ csSpecialCharacters: array [0 .. 51 ] of string = (' á' , ' à' , ' ã' , ' â' , ' ä' , ' é' , ' è' ,
22+ ' ê' , ' ë' , ' í' , ' ì' , ' ï' , ' î' , ' ó' , ' ò' , ' õ' , ' ô' , ' ö' , ' ú' , ' ù' , ' ü' , ' û' , ' ç' , ' ñ' ,
23+ ' ý' , ' ÿ' , ' Á' , ' À' , ' Ã' , ' Â' , ' Ä' , ' É' , ' È' , ' Ê' , ' Ë' , ' Í' , ' Ì' , ' Ï' , ' Î' , ' Ó' , ' Ò' ,
24+ ' Õ' , ' Ô' , ' Ö' , ' Ú' , ' Ù' , ' Ü' , ' Û' , ' Ç' , ' Ñ' , ' Ý' , ' Ÿ' );
25+ csRegularCharacters: array [0 .. 51 ] of string = (' a' , ' a' , ' a' , ' a' , ' a' , ' e' , ' e' ,
26+ ' e' , ' e' , ' i' , ' i' , ' i' , ' i' , ' o' , ' o' , ' o' , ' o' , ' o' , ' u' , ' u' , ' u' , ' u' , ' c' , ' n' ,
27+ ' y' , ' y' , ' A' , ' A' , ' A' , ' A' , ' A' , ' E' , ' E' , ' E' , ' E' , ' I' , ' I' , ' I' , ' I' , ' O' , ' O' ,
28+ ' O' , ' O' , ' O' , ' U' , ' U' , ' U' , ' U' , ' C' , ' N' , ' Y' , ' Y' );
1229
1330type
14- TFormato = (&Date, Bits, CEP, CEST, CFOP, CNH, CNPJ, CNPJorCPF, CPF, CREA,
15- CRM, Dinheiro, Hora, hhmm, InscricaoEstadual, NCM, OAB, Personalizado, Peso,
16- Porcentagem, Telefone, TituloEleitor, Valor, VeiculoMercosul,
17- VeiculoTradicional);
31+ TFormato = (&Date, Bits, CEP, CEST, CFOP, CNH, CNPJ, CNPJorCPF, CPF, CREA, CRM,
32+ Dinheiro, Hora, HoraCurta, InscricaoEstadual, NCM, OAB, Personalizado, Peso,
33+ Porcentagem, Telefone, TituloEleitor, Valor, VeiculoMercosul, VeiculoTradicional);
1834
1935 // estados da federação 0..26 = 27 ok
2036 // TODO: acrescentar código IBGE como índice padrão das siglas para facilidade de acesso
21- TUF = (AC, AL, AM, AP, BA, CE, DF, ES, GO, MA, MG, MT, MS, PA, PB, PE, PI, PR,
22- RJ, RN, RO, RR, RS, SC, SE, SP, &TO );
37+ TUF = (AC, AL, AM, AP, BA, CE, DF, ES, GO, MA, MG, MT, MS, PA, PB, PE, PI, PR, RJ, RN,
38+ RO, RR, RS, SC, SE, SP, &TO );
2339
2440 TFormatHelper = class
2541 private
@@ -43,6 +59,7 @@ TFormatHelper = class
4359 : string; overload;
4460 function Inteiro (aStr: string): string;
4561 function Primeiros (aStr: string; aDigitos: integer): string;
62+ function RemoveAcentos (aStr: string): string;
4663 function SomenteNumero (aStr: string): string;
4764 function Ultimos (aStr: string; aDigitos: integer): string;
4865 end ;
@@ -54,6 +71,7 @@ TEditHelper = class helper for TEdit
5471 procedure Formatar (aFormato: TFormato); overload;
5572 procedure Formatar (aFormato: TFormato; ExtraArg: Variant); overload;
5673 function Inteiro : string;
74+ function RemoveAcentos (aStr: string): string;
5775 function SomenteNumero : string;
5876 end ;
5977
@@ -64,6 +82,7 @@ TLabelHelper = class helper for TLabel
6482 procedure Formatar (aFormato: TFormato); overload;
6583 procedure Formatar (aFormato: TFormato; ExtraArg: Variant); overload;
6684 function Inteiro : string;
85+ function RemoveAcentos (aStr: string): string;
6786 function SomenteNumero : string;
6887 end ;
6988
@@ -74,21 +93,10 @@ TTextHelper = class helper for TText
7493 procedure Formatar (aFormato: TFormato); overload;
7594 procedure Formatar (aFormato: TFormato; ExtraArg: Variant); overload;
7695 function Inteiro : string;
96+ function RemoveAcentos (aStr: string): string;
7797 function SomenteNumero : string;
7898 end ;
7999
80- const
81- csNumbers = [' 0' .. ' 9' ];
82- csIntegers = [' 0' .. ' 9' , ' -' ];
83- csCharacters = [' a' .. ' z' , ' A' .. ' Z' ];
84- csCurrencyDigits = [' 0' .. ' 1' , ' -' , ' ,' ];
85- csLatinAccentCharacters = [' ã' , ' á' , ' à' , ' â' , ' é' , ' ê' , ' í' , ' ó' , ' ô' , ' õ' ,
86- ' ú' , ' ç' , ' Ã' , ' Á' , ' À' , ' Â' , ' É' , ' Ê' , ' Í' , ' Ó' , ' Õ' , ' Ô' , ' Ú' , ' Ç' ];
87- csLatinAlphaNumeric = [' 0' .. ' 9' , ' a' .. ' z' , ' A' .. ' Z' , ' ã' , ' á' , ' à' , ' â' ,
88- ' é' , ' ê' , ' í' , ' ó' , ' ô' , ' õ' , ' ú' , ' ç' , ' Ã' , ' Á' , ' À' , ' Â' , ' É' , ' Ê' , ' Í' ,
89- ' Ó' , ' Õ' , ' Ô' , ' Ú' , ' Ç' ];
90- csFormatIdentifier = [' #' , ' L' , ' l' , ' 9' ];
91-
92100var
93101 Formato: TFormatHelper;
94102
@@ -100,22 +108,22 @@ implementation
100108// / <returns>Devolve letras e números bem como caracteres acentuados.</returns>
101109function TFormatHelper.AlfaNumerico (aStr: string): string;
102110var
103- x : integer;
111+ I : integer;
104112begin
105113 Result := ' ' ;
106- for x := 0 to pred(aStr.Length) do
107- if CharInSet(aStr.Chars[x ], csLatinAlphaNumeric ) then
108- Result := Result + aStr.Chars[x ];
114+ for I := 0 to pred(aStr.Length) do
115+ if not CharInSet(aStr[I ], csSymbols ) then
116+ Result := Result + aStr[I ];
109117end ;
110118
111119function TFormatHelper.Decimal (aStr: string): string;
112120var
113- x : integer;
121+ I : integer;
114122begin
115123 Result := ' ' ;
116- for x := 0 to pred(aStr.Length) do
117- if CharInSet(aStr.Chars[x ], csCurrencyDigits) then
118- Result := Result + aStr.Chars[x ];
124+ for I := 0 to pred(aStr.Length) do
125+ if CharInSet(aStr.Chars[I ], csCurrencyDigits) then
126+ Result := Result + aStr.Chars[I ];
119127end ;
120128
121129// / <returns>
@@ -124,23 +132,19 @@ function TFormatHelper.Decimal(aStr: string): string;
124132// / <param name="aPrecisao"> Precisão de decimais ajustável. Valor padrão: 2 </param>
125133function TFormatHelper.Decimal (aStr: string; aPrecisao: integer): Double;
126134var
127- x : integer;
135+ I : integer;
128136 Valor: string;
129137begin
130138 if aPrecisao < 0 then
131139 aPrecisao := 2 ;
132140
133- try
134- Valor := ' ' ;
135- for x := 0 to pred(aStr.Length) do
136- if CharInSet(aStr.Chars[x], csCurrencyDigits) then
137- Valor := Valor + aStr.Chars[x];
141+ Valor := ' ' ;
142+ for I := 0 to pred(aStr.Length) do
143+ if CharInSet(aStr.Chars[I], csCurrencyDigits) then
144+ Valor := Valor + aStr.Chars[I];
138145
139- Result := Format(' %.' + aPrecisao.ToString + ' f' , [StrToFloatDef(Valor, 0 )]
140- ).ToDouble;
141- except
142- Result := Format(' %.' + aPrecisao.ToString + ' f' , [0 ]).ToDouble;
143- end ;
146+ Result := StrToFloatDef(Format(' %.' + IntToStr(aPrecisao) + ' f' ,
147+ [StrToFloatDef(Valor, 0 )]), 0 );
144148end ;
145149
146150// / <returns>
@@ -197,8 +201,7 @@ function TFormatHelper.FormataCRM(aStr: integer; UF: TUF): string;
197201begin
198202 if not (aStr = 0 ) then
199203 try
200- Result := Format(' CRM/%s %.6d' ,
201- [GetEnumName(TypeInfo(TUF), ord(UF)), aStr]);
204+ Result := Format(' CRM/%s %.6d' , [GetEnumName(TypeInfo(TUF), ord(UF)), aStr]);
202205 // Mask('CRM/LL ######', GetEnumName(TypeInfo(TUF), ord(UF)) +
203206 // Primeiros(aStr));
204207 except
@@ -244,8 +247,7 @@ function TFormatHelper.FormataData(aStr: string): string;
244247// / <returns>
245248// / Formata o texto para Dinheiro com precisão de decimais
246249// / </returns>
247- function TFormatHelper.FormataDinheiro (aStr: string;
248- aPrecisao: integer): string;
250+ function TFormatHelper.FormataDinheiro (aStr: string; aPrecisao: integer): string;
249251begin
250252 try
251253 Result := Format(' %.' + aPrecisao.ToString + ' m' ,
@@ -261,8 +263,7 @@ function TFormatHelper.FormataDinheiro(aStr: string;
261263function TFormatHelper.FormataHoraCurta (aStr: string): string;
262264begin
263265 try
264- if (aStr.IsEmpty) or
265- ((aStr.Length > 1 ) and (strtoint(Copy(aStr, 0 , 2 )) > 23 )) or
266+ if (aStr.IsEmpty) or ((aStr.Length > 1 ) and (strtoint(Copy(aStr, 0 , 2 )) > 23 )) or
266267 ((aStr.Length > 3 ) and (strtoint(Copy(aStr, 3 , 2 )) > 59 )) then
267268 Result := ' '
268269 else
@@ -278,8 +279,7 @@ function TFormatHelper.FormataHoraCurta(aStr: string): string;
278279function TFormatHelper.FormataHora (aStr: string): string;
279280begin
280281 try
281- if (aStr.IsEmpty) or
282- ((aStr.Length > 1 ) and (strtoint(Copy(aStr, 0 , 2 )) > 23 )) or
282+ if (aStr.IsEmpty) or ((aStr.Length > 1 ) and (strtoint(Copy(aStr, 0 , 2 )) > 23 )) or
283283 ((aStr.Length > 3 ) and (strtoint(Copy(aStr, 3 , 2 )) > 59 )) or
284284 ((aStr.Length > 5 ) and (strtoint(Copy(aStr, 5 , 2 )) > 59 )) then
285285 Result := ' '
@@ -375,8 +375,7 @@ function TFormatHelper.FormataOAB(aStr: integer; UF: TUF): string;
375375// / <returns>
376376// / Formata o texto em um número com 3 casas decimais
377377// / </returns>
378- function TFormatHelper.FormataPeso (aStr: string;
379- aSeparador: boolean = false): string;
378+ function TFormatHelper.FormataPeso (aStr: string; aSeparador: boolean = false): string;
380379begin
381380 try
382381 if aSeparador then
@@ -435,7 +434,7 @@ function TFormatHelper.Formatar(Formato: TFormato; Texto: string;
435434 Texto := Mask(' 999999999-9' , SomenteNumero(Texto));
436435
437436 CRM:
438- Texto := FormataCRM(Ultimos(SomenteNumero(Texto), 6 ).ToInteger , ExtraArg);
437+ Texto := FormataCRM(StrToIntDef( Ultimos(SomenteNumero(Texto), 6 ), 0 ) , ExtraArg);
439438
440439 Dinheiro:
441440 if ExtraArg <> varNull then
@@ -446,7 +445,7 @@ function TFormatHelper.Formatar(Formato: TFormato; Texto: string;
446445 Hora:
447446 Texto := FormataHora(SomenteNumero(Texto));
448447
449- hhmm :
448+ HoraCurta :
450449 Texto := FormataHoraCurta(SomenteNumero(Texto));
451450
452451 InscricaoEstadual:
@@ -456,7 +455,7 @@ function TFormatHelper.Formatar(Formato: TFormato; Texto: string;
456455 Texto := Mask(' 9999.99.99' , SomenteNumero(Texto));
457456
458457 OAB:
459- Texto := FormataOAB(Ultimos(SomenteNumero(Texto), 6 ).ToInteger , ExtraArg);
458+ Texto := FormataOAB(StrToIntDef( Ultimos(SomenteNumero(Texto), 6 ), 0 ) , ExtraArg);
460459
461460 Personalizado:
462461 Texto := Mask(ExtraArg, SomenteNumero(Texto));
@@ -465,7 +464,7 @@ function TFormatHelper.Formatar(Formato: TFormato; Texto: string;
465464 Texto := FormataPeso(SomenteNumero(Texto));
466465
467466 Porcentagem:
468- Texto := Format(' %.2f %s ' , [Decimal(Texto, 2 ), ' % ' ]);
467+ Texto := Format(' %.2f %% ' , [Decimal(Texto, 2 )]);
469468
470469 Telefone:
471470 if Length(SomenteNumero(Texto)) <= 10 then
@@ -509,12 +508,12 @@ function TFormatHelper.FormataValor(aStr: string; aSeparador: boolean): string;
509508// / </returns>
510509function TFormatHelper.Inteiro (aStr: string): string;
511510var
512- x : integer;
511+ I : integer;
513512begin
514513 Result := ' ' ;
515- for x := 0 to Length(aStr) - 1 do
516- if CharInSet(aStr.Chars[x ], csIntegers) then
517- Result := Result + aStr.Chars[x ];
514+ for I := 0 to Length(aStr) - 1 do
515+ if CharInSet(aStr.Chars[I ], csIntegers) then
516+ Result := Result + aStr.Chars[I ];
518517end ;
519518
520519// / <returns>
@@ -552,8 +551,8 @@ function TFormatHelper.Mask(Mascara, aStr: string): string;
552551 Result := Result + LowerCase(aStr.Chars[textidx]);
553552 inc(textidx);
554553 end
555- else if (Mascara.Chars[maskidx] = ' 9' ) and CharInSet(aStr.Chars[textidx],
556- csNumbers) then
554+ else if (Mascara.Chars[maskidx] = ' 9' ) and CharInSet(aStr.Chars[textidx], csNumbers)
555+ then
557556 begin
558557 Result := Result + aStr.Chars[textidx];
559558 inc(textidx);
@@ -575,6 +574,16 @@ function TFormatHelper.Primeiros(aStr: string; aDigitos: integer): string;
575574 Result := Copy(aStr, 1 , aDigitos);
576575end ;
577576
577+ function TFormatHelper.RemoveAcentos (aStr: string): string;
578+ var
579+ I: integer;
580+ begin
581+ Result := aStr;
582+ for I := 0 to pred(Length(csSpecialCharacters)) do
583+ Result := StringReplace(Result, csSpecialCharacters[I], csRegularCharacters[I],
584+ [rfReplaceAll]);
585+ end ;
586+
578587// / <returns>
579588// / Devolve somente os números contidos no texto
580589// / </returns>
@@ -623,6 +632,11 @@ function TEditHelper.Inteiro: string;
623632 Self.GoToTextEnd;
624633end ;
625634
635+ function TEditHelper.RemoveAcentos (aStr: string): string;
636+ begin
637+ Result := Formato.RemoveAcentos(Self.Text);
638+ end ;
639+
626640procedure TEditHelper.Formatar (aFormato: TFormato; ExtraArg: Variant);
627641begin
628642 Self.Text := Formato.Formatar(aFormato, Self.Text, ExtraArg);
@@ -662,6 +676,11 @@ function TTextHelper.Inteiro: string;
662676 Result := Formato.Inteiro(Self.Text);
663677end ;
664678
679+ function TTextHelper.RemoveAcentos (aStr: string): string;
680+ begin
681+ Result := Formato.RemoveAcentos(Self.Text);
682+ end ;
683+
665684function TTextHelper.SomenteNumero : string;
666685begin
667686 Result := Formato.SomenteNumero(Self.Text);
@@ -694,6 +713,11 @@ function TLabelHelper.Inteiro: string;
694713 Result := Formato.Inteiro(Self.Text);
695714end ;
696715
716+ function TLabelHelper.RemoveAcentos (aStr: string): string;
717+ begin
718+ Result := Formato.RemoveAcentos(Self.Text);
719+ end ;
720+
697721function TLabelHelper.SomenteNumero : string;
698722begin
699723 Result := Formato.SomenteNumero(Self.Text);
0 commit comments