Create a new [`Ident`] modifying the given value as necessary to meet our invariants. # Examples ``` use mz_sql_parser::ast::Ident; let too_long = "🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢\ 🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵\ 🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴🔴"; let id = Ident::new_lossy(too_long); // `new_lossy` will truncate the provided string, since it's too long. Note the missing // `🔴` characters.
(value: S)
| 103 | /// assert_eq!(id.as_str(), "🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🟢🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵🔵"); |
| 104 | /// ``` |
| 105 | pub fn new_lossy<S: Into<String>>(value: S) -> Self { |
| 106 | let s: String = value.into(); |
| 107 | if s.len() <= Self::MAX_LENGTH { |
| 108 | return Ident(s); |
| 109 | } |
| 110 | |
| 111 | let mut byte_length = 0; |
| 112 | let s_truncated = s |
| 113 | .chars() |
| 114 | .take_while(|c| { |
| 115 | byte_length += c.len_utf8(); |
| 116 | byte_length <= Self::MAX_LENGTH |
| 117 | }) |
| 118 | .collect(); |
| 119 | |
| 120 | Ident(s_truncated) |
| 121 | } |
| 122 | |
| 123 | /// Create a new [`Ident`] _without checking any of our invariants_. |
| 124 | /// |
nothing calls this directly
no test coverage detected