Masks the given unicode code point depending on its range and the (optional) given locale. By default, if no locale is provided, i.e. loc == nullptr, lowercase/uppercase/digit characters are only recognized in ascii character set. UNMASKED_VAL(-1) means keeping the original value. Returns the masked code point.
| 53 | /// UNMASKED_VAL(-1) means keeping the original value. |
| 54 | /// Returns the masked code point. |
| 55 | static inline uint32_t MaskTransform(uint32_t val, int masked_upper_char, |
| 56 | int masked_lower_char, int masked_digit_char, int masked_other_char, |
| 57 | const std::locale* loc = nullptr) { |
| 58 | // Fast code path for masking ascii characters only. |
| 59 | if (loc == nullptr) { |
| 60 | if ('A' <= val && val <= 'Z') { |
| 61 | if (masked_upper_char == UNMASKED_VAL) return val; |
| 62 | return masked_upper_char; |
| 63 | } |
| 64 | if ('a' <= val && val <= 'z') { |
| 65 | if (masked_lower_char == UNMASKED_VAL) return val; |
| 66 | return masked_lower_char; |
| 67 | } |
| 68 | if ('0' <= val && val <= '9') { |
| 69 | if (masked_digit_char == UNMASKED_VAL) return val; |
| 70 | return masked_digit_char; |
| 71 | } |
| 72 | if (masked_other_char == UNMASKED_VAL) return val; |
| 73 | return masked_other_char; |
| 74 | } |
| 75 | // Check facet existence to avoid predicates throws exception. |
| 76 | DCHECK(std::has_facet<std::ctype<wchar_t>>(*loc)) |
| 77 | << "Facet not found for locale " << loc->name(); |
| 78 | if (isupper((wchar_t)val, *loc)) { |
| 79 | if (masked_upper_char == UNMASKED_VAL) return val; |
| 80 | return masked_upper_char; |
| 81 | } |
| 82 | if (islower((wchar_t)val, *loc)) { |
| 83 | if (masked_lower_char == UNMASKED_VAL) return val; |
| 84 | return masked_lower_char; |
| 85 | } |
| 86 | if (isdigit((wchar_t)val, *loc)) { |
| 87 | if (masked_digit_char == UNMASKED_VAL) return val; |
| 88 | return masked_digit_char; |
| 89 | } |
| 90 | if (masked_other_char == UNMASKED_VAL) return val; |
| 91 | return masked_other_char; |
| 92 | } |
| 93 | |
| 94 | /// Mask the substring in range [start, end) of the given string value. Using rules in |
| 95 | /// 'MaskTransform'. Indices are counted in bytes. |
no test coverage detected