MCPcopy Create free account
hub / github.com/MaterializeInc/materialize / new_lossy

Method new_lossy

src/sql-parser/src/ast/defs/name.rs:105–121  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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 ///

Callers

nothing calls this directly

Calls 4

IdentClass · 0.85
take_whileMethod · 0.80
lenMethod · 0.45
collectMethod · 0.45

Tested by

no test coverage detected