Function git_config::value::normalize_bstring
source · Expand description
Vec[u8]
variant of normalize
.
Examples found in repository?
src/file/section/body.rs (line 46)
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
pub fn value_implicit(&self, key: impl AsRef<str>) -> Option<Option<Cow<'_, BStr>>> {
let key = Key::from_str_unchecked(key.as_ref());
let (_key_range, range) = self.key_and_value_range_by(&key)?;
let range = match range {
None => return Some(None),
Some(range) => range,
};
let mut concatenated = BString::default();
for event in &self.0[range] {
match event {
Event::Value(v) => {
return Some(Some(normalize_bstr(v.as_ref())));
}
Event::ValueNotDone(v) => {
concatenated.push_str(v.as_ref());
}
Event::ValueDone(v) => {
concatenated.push_str(v.as_ref());
return Some(Some(normalize_bstring(concatenated)));
}
_ => (),
}
}
None
}
/// Retrieves all values that have the provided key name. This may return
/// an empty vec, which implies there were no values with the provided key.
#[must_use]
pub fn values(&self, key: impl AsRef<str>) -> Vec<Cow<'_, BStr>> {
let key = &Key::from_str_unchecked(key.as_ref());
let mut values = Vec::new();
let mut expect_value = false;
let mut concatenated_value = BString::default();
for event in &self.0 {
match event {
Event::SectionKey(event_key) if event_key == key => expect_value = true,
Event::Value(v) if expect_value => {
expect_value = false;
values.push(normalize_bstr(v.as_ref()));
}
Event::ValueNotDone(v) if expect_value => {
concatenated_value.push_str(v.as_ref());
}
Event::ValueDone(v) if expect_value => {
expect_value = false;
concatenated_value.push_str(v.as_ref());
values.push(normalize_bstring(std::mem::take(&mut concatenated_value)));
}
_ => (),
}
}
values
}
More examples
src/value/normalize.rs (line 69)
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
pub fn normalize(input: Cow<'_, BStr>) -> Cow<'_, BStr> {
if input.as_ref() == "\"\"" {
return Cow::Borrowed("".into());
}
let size = input.len();
if size >= 3 && input[0] == b'"' && input[size - 1] == b'"' && input[size - 2] != b'\\' {
match input {
Cow::Borrowed(input) => return normalize_bstr(&input[1..size - 1]),
Cow::Owned(mut input) => {
input.pop();
input.remove(0);
return normalize_bstring(input);
}
}
}
if input.find_byteset(b"\\\"").is_none() {
return input;
}
let mut out: BString = Vec::with_capacity(input.len()).into();
let mut bytes = input.iter().copied();
while let Some(c) = bytes.next() {
match c {
b'\\' => match bytes.next() {
Some(b'n') => out.push(b'\n'),
Some(b't') => out.push(b'\t'),
Some(b'b') => {
out.pop();
}
Some(c) => {
out.push(c);
}
None => break,
},
b'"' => {}
_ => out.push(c),
}
}
Cow::Owned(out)
}
src/file/mutable/section.rs (lines 105-111)
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
pub fn pop(&mut self) -> Option<(Key<'_>, Cow<'event, BStr>)> {
let mut values = Vec::new();
// events are popped in reverse order
let body = &mut self.section.body.0;
while let Some(e) = body.pop() {
match e {
Event::SectionKey(k) => {
// pop leading whitespace
if let Some(Event::Whitespace(_)) = body.last() {
body.pop();
}
if values.len() == 1 {
let value = values.pop().expect("vec is non-empty but popped to empty value");
return Some((k, normalize(value)));
}
return Some((
k,
normalize_bstring({
let mut s = BString::default();
for value in values.into_iter().rev() {
s.push_str(value.as_ref());
}
s
}),
));
}
Event::Value(v) | Event::ValueNotDone(v) | Event::ValueDone(v) => values.push(v),
_ => (),
}
}
None
}
/// Sets the last key value pair if it exists, or adds the new value.
/// Returns the previous value if it replaced a value, or None if it adds
/// the value.
pub fn set<'b>(&mut self, key: Key<'event>, value: impl Into<&'b BStr>) -> Option<Cow<'event, BStr>> {
match self.key_and_value_range_by(&key) {
None => {
self.push(key, Some(value.into()));
None
}
Some((key_range, value_range)) => {
let value_range = value_range.unwrap_or(key_range.end - 1..key_range.end);
let range_start = value_range.start;
let ret = self.remove_internal(value_range, false);
self.section
.body
.0
.insert(range_start, Event::Value(escape_value(value.into()).into()));
Some(ret)
}
}
}
/// Removes the latest value by key and returns it, if it exists.
pub fn remove(&mut self, key: impl AsRef<str>) -> Option<Cow<'event, BStr>> {
let key = Key::from_str_unchecked(key.as_ref());
let (key_range, _value_range) = self.key_and_value_range_by(&key)?;
Some(self.remove_internal(key_range, true))
}
/// Adds a new line event. Note that you don't need to call this unless
/// you've disabled implicit newlines.
pub fn push_newline(&mut self) {
self.section
.body
.0
.push(Event::Newline(Cow::Owned(BString::from(self.newline.to_vec()))));
}
/// Return the newline used when calling [`push_newline()`][Self::push_newline()].
pub fn newline(&self) -> &BStr {
self.newline.as_slice().as_bstr()
}
/// Enables or disables automatically adding newline events after adding
/// a value. This is _enabled by default_.
pub fn set_implicit_newline(&mut self, on: bool) {
self.implicit_newline = on;
}
/// Sets the exact whitespace to use before each newly created key-value pair,
/// with only whitespace characters being permissible.
///
/// The default is 2 tabs.
/// Set to `None` to disable adding whitespace before a key value.
///
/// # Panics
///
/// If non-whitespace characters are used. This makes the method only suitable for validated
/// or known input.
pub fn set_leading_whitespace(&mut self, whitespace: Option<Cow<'event, BStr>>) {
assert!(
whitespace
.as_deref()
.map_or(true, |ws| ws.iter().all(|b| b.is_ascii_whitespace())),
"input whitespace must only contain whitespace characters."
);
self.whitespace.pre_key = whitespace;
}
/// Returns the whitespace this section will insert before the
/// beginning of a key, if any.
#[must_use]
pub fn leading_whitespace(&self) -> Option<&BStr> {
self.whitespace.pre_key.as_deref()
}
/// Returns the whitespace to be used before and after the `=` between the key
/// and the value.
///
/// For example, `k = v` will have `(Some(" "), Some(" "))`, whereas `k=\tv` will
/// have `(None, Some("\t"))`.
#[must_use]
pub fn separator_whitespace(&self) -> (Option<&BStr>, Option<&BStr>) {
(self.whitespace.pre_sep.as_deref(), self.whitespace.post_sep.as_deref())
}
}
// Internal methods that may require exact indices for faster operations.
impl<'a, 'event> SectionMut<'a, 'event> {
pub(crate) fn new(section: &'a mut Section<'event>, newline: SmallVec<[u8; 2]>) -> Self {
let whitespace = Whitespace::from_body(§ion.body);
Self {
section,
implicit_newline: true,
whitespace,
newline,
}
}
pub(crate) fn get<'key>(
&self,
key: &Key<'key>,
start: Index,
end: Index,
) -> Result<Cow<'_, BStr>, lookup::existing::Error> {
let mut expect_value = false;
let mut concatenated_value = BString::default();
for event in &self.section.0[start.0..end.0] {
match event {
Event::SectionKey(event_key) if event_key == key => expect_value = true,
Event::Value(v) if expect_value => return Ok(normalize_bstr(v.as_ref())),
Event::ValueNotDone(v) if expect_value => {
concatenated_value.push_str(v.as_ref());
}
Event::ValueDone(v) if expect_value => {
concatenated_value.push_str(v.as_ref());
return Ok(normalize_bstring(concatenated_value));
}
_ => (),
}
}
Err(lookup::existing::Error::KeyMissing)
}
src/file/mutable/multi_value.rs (line 62)
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
pub fn get(&self) -> Result<Vec<Cow<'_, BStr>>, lookup::existing::Error> {
let mut expect_value = false;
let mut values = Vec::new();
let mut concatenated_value = BString::default();
for EntryData {
section_id,
offset_index,
} in &self.indices_and_sizes
{
let (offset, size) = MultiValueMut::index_and_size(&self.offsets, *section_id, *offset_index);
for event in &self.section.get(section_id).expect("known section id").as_ref()[offset..offset + size] {
match event {
Event::SectionKey(section_key) if *section_key == self.key => expect_value = true,
Event::Value(v) if expect_value => {
expect_value = false;
values.push(normalize_bstr(v.as_ref()));
}
Event::ValueNotDone(v) if expect_value => concatenated_value.push_str(v.as_ref()),
Event::ValueDone(v) if expect_value => {
expect_value = false;
concatenated_value.push_str(v.as_ref());
values.push(normalize_bstring(std::mem::take(&mut concatenated_value)));
}
_ => (),
}
}
}
if values.is_empty() {
return Err(lookup::existing::Error::KeyMissing);
}
Ok(values)
}