|
1 | | -using System; |
2 | | -using System.Text; |
3 | | -using System.Text.RegularExpressions; |
| 1 | +namespace Sirb.Validation.Benchmark.TestLib; |
4 | 2 |
|
5 | | -namespace Sirb.Validation.Benchmark.TestLib |
| 3 | +static public class StringTest |
6 | 4 | { |
7 | | - public static class StringTest |
| 5 | + static public string RemoveMaskWithReplace(string cpf) |
8 | 6 | { |
9 | | - public static string RemoveMaskWithReplace(string cpf) |
10 | | - { |
11 | | - if (string.IsNullOrEmpty(cpf?.Trim())) |
12 | | - return default; |
13 | | - |
14 | | - return cpf.Trim().Replace(".", "").Replace("-", ""); |
15 | | - } |
| 7 | + if (string.IsNullOrEmpty(cpf?.Trim())) |
| 8 | + return null; |
16 | 9 |
|
17 | | - public static string RemoveMaskWithFor(string cpf) |
18 | | - { |
19 | | - if (string.IsNullOrEmpty(cpf?.Trim())) |
20 | | - return default; |
| 10 | + return cpf.Trim() |
| 11 | + .Replace(".", string.Empty) |
| 12 | + .Replace("-", string.Empty); |
| 13 | + } |
21 | 14 |
|
22 | | - for (int i = cpf.Length; i > 0; i--) |
23 | | - { |
24 | | - if (char.IsNumber(cpf[i - 1])) |
25 | | - continue; |
| 15 | + static public string RemoveMaskWithFor(string cpf) |
| 16 | + { |
| 17 | + if (string.IsNullOrEmpty(cpf?.Trim())) |
| 18 | + return default; |
26 | 19 |
|
27 | | - cpf = cpf.Remove(i - 1); |
28 | | - } |
| 20 | + for (var i = cpf.Length; i > 0; i--) |
| 21 | + { |
| 22 | + if (char.IsNumber(cpf[i - 1])) |
| 23 | + continue; |
29 | 24 |
|
30 | | - return cpf; |
| 25 | + cpf = cpf.Remove(i - 1); |
31 | 26 | } |
32 | 27 |
|
33 | | - public static string RemoveMaskWithRegex(string cpf) |
34 | | - { |
35 | | - if (string.IsNullOrEmpty(cpf?.Trim())) |
36 | | - return default; |
| 28 | + return cpf; |
| 29 | + } |
37 | 30 |
|
38 | | - return Regex.Replace(cpf, @"[^\d]", string.Empty); |
39 | | - } |
| 31 | + static public string RemoveMaskWithRegex(string cpf) |
| 32 | + { |
| 33 | + if (string.IsNullOrEmpty(cpf?.Trim())) |
| 34 | + return default; |
40 | 35 |
|
41 | | - public static string Reverse1(string value) |
42 | | - { |
43 | | - char[] charArray = value.ToCharArray(); |
44 | | - Array.Reverse(charArray); |
45 | | - return new string(charArray); |
46 | | - } |
| 36 | + return Regex.Replace(cpf, @"[^\d]", string.Empty); |
| 37 | + } |
47 | 38 |
|
48 | | - public static string Reverse2(string value) |
49 | | - { |
50 | | - if (string.IsNullOrEmpty(value)) return value; |
| 39 | + static public string Reverse1(string value) |
| 40 | + { |
| 41 | + var charArray = value.ToCharArray(); |
| 42 | + Array.Reverse(charArray); |
| 43 | + return new string(charArray); |
| 44 | + } |
51 | 45 |
|
52 | | - StringBuilder sb = new StringBuilder(); |
53 | | - for (int i = value.Length; i > 0; i--) |
54 | | - { |
55 | | - _ = sb.Append(value[i - 1]); |
56 | | - } |
| 46 | + static public string Reverse2(string value) |
| 47 | + { |
| 48 | + if (string.IsNullOrEmpty(value)) return value; |
57 | 49 |
|
58 | | - return sb.ToString(); |
59 | | - } |
| 50 | + var sb = new StringBuilder(); |
| 51 | + for (var i = value.Length; i > 0; i--) |
| 52 | + _ = sb.Append(value[i - 1]); |
| 53 | + |
| 54 | + return sb.ToString(); |
60 | 55 | } |
61 | 56 | } |
0 commit comments