re2 0.0.11

Wrapper for the re2 C++ regex library.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
84
85
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/* Copyright 2023 Danny McClanahan */
/* SPDX-License-Identifier: BSD-3-Clause */

//! Wrappers for string data that interact with C++.
//!
//! [`StringView`] is widely interchangeable with [`str`](prim@str) throughout
//! this crate in order to operate on non-UTF-8 borrowed text, and is used to
//! implement the [`str`](prim@str) interface. [`StringWrapper`] on the other
//! hand is used to manage owned string data, and can be resized as well as
//! converted into a [`StringView`] or [`StringMut`] to avoid reallocating
//! memory while retaining a C++-compatible string.

use crate::re2_c;
#[cfg(doc)]
use crate::RE2;

use std::{cmp, fmt, hash, marker::PhantomData, mem, ops, os::raw::c_char, ptr, slice, str};

/// FFI-compatible reference to a read-only slice of bytes.
///
///```
/// use re2::string::StringView;
///
/// let s: StringView = "asdf".into();
/// assert_eq!(s, StringView::from_str("asdf"));
/// assert_eq!(unsafe { s.as_str() }, "asdf");
/// assert_eq!(s, StringView::from_slice(b"asdf"));
/// assert_eq!(s.as_slice(), b"asdf");
/// ```
#[derive(Copy, Clone)]
#[repr(transparent)]
pub struct StringView<'a> {
  inner: re2_c::StringView,
  _ph: PhantomData<&'a u8>,
}

impl<'a> StringView<'a> {
  /// Apply an indexing operation to extract a subset of this byte slice.
  ///
  ///```
  /// use re2::string::StringView;
  ///
  /// let mut s = StringView::from_str("asdf");
  /// s = s.index_range(0..2).unwrap();
  /// assert_eq!(s.as_slice(), b"as");
  /// ```
  pub fn index_range(&self, range: impl slice::SliceIndex<[u8], Output=[u8]>) -> Option<Self> {
    self.as_slice().get(range).map(Self::from_slice)
  }

  /// Produce an empty string.
  ///
  ///```
  /// use re2::string::StringView;
  ///
  /// assert_eq!(unsafe { StringView::empty().as_str() }, "");
  /// ```
  pub const fn empty() -> Self {
    let inner = re2_c::StringView {
      data_: ptr::null(),
      len_: 0,
    };
    Self::from_native(inner)
  }

  pub(crate) const fn from_native(inner: re2_c::StringView) -> Self {
    Self {
      inner,
      _ph: PhantomData,
    }
  }

  pub(crate) const fn into_native(self) -> re2_c::StringView { self.inner }

  /// Extract an FFI-compatible representation of a byte slice.
  pub const fn from_slice(b: &'a [u8]) -> Self {
    let inner = re2_c::StringView {
      data_: b.as_ptr() as *const c_char,
      len_: b.len(),
    };
    Self::from_native(inner)
  }

  /// Extract an FFI-compatible representation of a string's backing byte slice.
  pub const fn from_str(s: &'a str) -> Self { Self::from_slice(s.as_bytes()) }

  const unsafe fn data_pointer(&self) -> *const u8 { mem::transmute(self.inner.data_) }

  /// Whether the slice has any data.
  pub const fn is_empty(&self) -> bool { self.len() == 0 }

  /// The number of bytes in the slice.
  pub const fn len(&self) -> usize { self.inner.len_ }

  /// Produce a Rust-compatible view of this byte slice.
  pub const fn as_slice(&self) -> &'a [u8] {
    if self.is_empty() {
      &[]
    } else {
      unsafe { slice::from_raw_parts(self.data_pointer(), self.len()) }
    }
  }

  /// Produce a Rust string view of this byte slice.
  ///
  /// # Safety
  /// The underlying bytes must be UTF-8 encoded. This is currently always safe,
  /// as this library only supports UTF-8 and Latin-1 encodings.
  pub const unsafe fn as_str(&self) -> &'a str { str::from_utf8_unchecked(self.as_slice()) }

  /* Used in "consume" methods which may update a string view to a substring
   * upon match. */
  pub(crate) fn as_mut_native(&mut self) -> &mut re2_c::StringView { &mut self.inner }
}

impl<'a> From<&'a [u8]> for StringView<'a> {
  fn from(x: &'a [u8]) -> Self { Self::from_slice(x) }
}

impl<'a, const N: usize> From<&'a [u8; N]> for StringView<'a> {
  fn from(x: &'a [u8; N]) -> Self { Self::from_slice(x.as_ref()) }
}

impl<'a> From<&'a str> for StringView<'a> {
  fn from(x: &'a str) -> Self { Self::from_str(x) }
}

impl<'a> Default for StringView<'a> {
  fn default() -> Self { Self::empty() }
}

impl<'a> fmt::Debug for StringView<'a> {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    let s = self.as_slice();
    match str::from_utf8(s) {
      Ok(s) => write!(f, "{:?}", s),
      Err(_) => write!(f, "{:?}", s),
    }
  }
}

impl<'a> fmt::Display for StringView<'a> {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    let s = self.as_slice();
    match str::from_utf8(s) {
      Ok(s) => write!(f, "{}", s),
      Err(_) => write!(f, "{:?}", s),
    }
  }
}

impl<'a> cmp::PartialEq for StringView<'a> {
  fn eq(&self, other: &Self) -> bool { self.as_slice().eq(other.as_slice()) }
}

impl<'a> cmp::Eq for StringView<'a> {}

impl<'a> cmp::PartialOrd for StringView<'a> {
  fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> { Some(self.cmp(other)) }
}

impl<'a> cmp::Ord for StringView<'a> {
  fn cmp(&self, other: &Self) -> cmp::Ordering { self.as_slice().cmp(other.as_slice()) }
}

impl<'a> hash::Hash for StringView<'a> {
  fn hash<H>(&self, state: &mut H)
  where H: hash::Hasher {
    self.as_slice().hash(state);
  }
}

/// FFI-compatible reference to a mutable slice of bytes.
///
///```
/// use re2::string::StringMut;
///
/// let mut s = "asdf".to_string();
/// let s1 = StringMut::from_mut_str(&mut s);
/// assert_eq!(unsafe { s1.as_mut_str() }, "asdf");
/// s1.as_mut_slice()[2] = b'e';
/// assert_eq!(s1.as_mut_slice(), b"asef");
///
/// let mut s2 = *b"asdf";
/// let s3 = StringMut::from_mut_slice(&mut s2);
/// s3.as_mut_slice()[2] = b'e';
/// assert_eq!(s3, s1);
/// ```
#[derive(Copy, Clone)]
#[repr(transparent)]
pub struct StringMut<'a> {
  inner: re2_c::StringMut,
  _ph: PhantomData<&'a mut u8>,
}

impl<'a> StringMut<'a> {
  /// Produce an empty string.
  ///
  ///```
  /// use re2::string::StringMut;
  ///
  /// assert_eq!(unsafe { StringMut::empty().as_mut_str() }, "");
  /// ```
  pub fn empty() -> Self {
    let inner = re2_c::StringMut {
      data_: ptr::null_mut(),
      len_: 0,
    };
    Self::from_native(inner)
  }

  /* NB: This can't be const because it references &mut data! */
  pub(crate) fn from_native(inner: re2_c::StringMut) -> Self {
    Self {
      inner,
      _ph: PhantomData,
    }
  }

  /* NB: This can't be const because it references &mut data! */
  /// Extract an FFI-compatible representation of a mutable byte slice.
  pub fn from_mut_slice(b: &'a mut [u8]) -> Self {
    let inner = re2_c::StringMut {
      data_: b.as_mut_ptr() as *mut c_char,
      len_: b.len(),
    };
    Self::from_native(inner)
  }

  /* NB: not const bc .as_bytes_mut() isn't const!! */
  /// Extract an FFI-compatible representation of a mutable string's backing
  /// byte slice.
  pub fn from_mut_str(s: &'a mut str) -> Self { Self::from_mut_slice(unsafe { s.as_bytes_mut() }) }

  const unsafe fn mut_data_pointer(&self) -> *mut u8 { mem::transmute(self.inner.data_) }

  /// Whether the slice has any data.
  pub const fn is_empty(&self) -> bool { self.len() == 0 }

  /// The number of bytes in the slice.
  pub const fn len(&self) -> usize { self.inner.len_ }

  /// Produce a Rust-compatible view of this mutable byte slice.
  pub fn as_mut_slice(&self) -> &'a mut [u8] {
    if self.is_empty() {
      &mut []
    } else {
      unsafe { slice::from_raw_parts_mut(self.mut_data_pointer(), self.len()) }
    }
  }

  /// Produce a Rust string view of this mutable byte slice.
  ///
  /// # Safety
  /// The underlying bytes must be UTF-8 encoded. This is currently always safe,
  /// as this library only supports UTF-8 and Latin-1 encodings.
  pub unsafe fn as_mut_str(&self) -> &'a mut str {
    str::from_utf8_unchecked_mut(self.as_mut_slice())
  }
}

impl<'a> From<&'a mut [u8]> for StringMut<'a> {
  fn from(x: &'a mut [u8]) -> Self { Self::from_mut_slice(x) }
}

impl<'a, const N: usize> From<&'a mut [u8; N]> for StringMut<'a> {
  fn from(x: &'a mut [u8; N]) -> Self { Self::from_mut_slice(x.as_mut()) }
}

impl<'a> From<&'a mut str> for StringMut<'a> {
  fn from(x: &'a mut str) -> Self { Self::from_mut_str(x) }
}

impl<'a> Default for StringMut<'a> {
  fn default() -> Self { Self::empty() }
}

impl<'a> fmt::Debug for StringMut<'a> {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    let s = self.as_mut_slice();
    match str::from_utf8(s) {
      Ok(s) => write!(f, "{:?}", s),
      Err(_) => write!(f, "{:?}", s),
    }
  }
}

impl<'a> fmt::Display for StringMut<'a> {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    let s = self.as_mut_slice();
    match str::from_utf8(s) {
      Ok(s) => write!(f, "{}", s),
      Err(_) => write!(f, "{:?}", s),
    }
  }
}

impl<'a> cmp::PartialEq for StringMut<'a> {
  fn eq(&self, other: &Self) -> bool { self.as_mut_slice().eq(&other.as_mut_slice()) }
}

impl<'a> cmp::Eq for StringMut<'a> {}

impl<'a> cmp::PartialOrd for StringMut<'a> {
  fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> { Some(self.cmp(other)) }
}

impl<'a> cmp::Ord for StringMut<'a> {
  fn cmp(&self, other: &Self) -> cmp::Ordering { self.as_mut_slice().cmp(&other.as_mut_slice()) }
}

impl<'a> hash::Hash for StringMut<'a> {
  fn hash<H>(&self, state: &mut H)
  where H: hash::Hasher {
    self.as_mut_slice().hash(state);
  }
}

/// Handle to a heap-allocated C++ `std::string`.
///
/// This is used as an output for some string search methods such as
/// [`RE2::extract()`] which need to allocate memory dynamically. The same
/// instance can be provided to multiple calls in order to avoid allocating a
/// new string upon each call, and [`Self::as_mut_view()`] and
/// [`Self::resize()`] can be used to modify the contents in between calls.
///
/// This object "owns" its string data, and upon destruction, this object will
/// deallocate the underlying `std::string` data as well. Use
/// [`Self::as_view()`] to extract a view of the underlying data in order to
/// copy it elsewhere.
#[derive(Debug)]
#[repr(transparent)]
pub struct StringWrapper(re2_c::StringWrapper);

impl StringWrapper {
  /// Generate an instance without allocating any memory dynamically.
  ///
  ///```
  /// let s = re2::string::StringWrapper::blank();
  /// assert_eq!(unsafe { s.as_view().as_str() }, "");
  /// ```
  pub const fn blank() -> Self {
    Self(re2_c::StringWrapper {
      inner_: ptr::null_mut(),
    })
  }

  /// Generate an instance which copies the bytes from the argument `s`.
  ///
  ///```
  /// let s = re2::string::StringWrapper::from_view("asdf".into());
  /// assert_eq!(unsafe { s.as_view().as_str() }, "asdf");
  /// ```
  pub fn from_view(s: StringView) -> Self {
    Self(unsafe { re2_c::StringWrapper::new(s.into_native()) })
  }

  pub(crate) fn as_mut_native(&mut self) -> &mut re2_c::StringWrapper { &mut self.0 }

  /// Generate a read-only view of the dynamically allocated memory.
  ///
  ///```
  /// let s = re2::string::StringWrapper::from_view("asdf".into());
  /// assert_eq!(unsafe { s.as_view().as_str() }, "asdf");
  /// ```
  pub fn as_view(&self) -> StringView { unsafe { StringView::from_native(self.0.as_view()) } }

  /// Generate a mutable view of the dynamically allocated memory.
  ///
  ///```
  /// let mut s = re2::string::StringWrapper::from_view("asdf".into());
  /// s.as_mut_view().as_mut_slice()[2] = b'e';
  /// assert_eq!(unsafe { s.as_view().as_str() }, "asef");
  /// ```
  pub fn as_mut_view(&mut self) -> StringMut<'_> {
    unsafe { StringMut::from_native(self.0.as_mut_view()) }
  }

  /// Resize the underlying `std::string` storage.
  ///
  /// If the new length is larger than the old, this method will pad the result
  /// with null bytes.
  ///
  ///```
  /// let mut s = re2::string::StringWrapper::from_view("asdf".into());
  /// assert_eq!(unsafe { s.as_view().as_str() }, "asdf");
  /// s.resize(2);
  /// assert_eq!(unsafe { s.as_view().as_str() }, "as");
  /// s.resize(6);
  /// assert_eq!(unsafe { s.as_view().as_str() }, "as\0\0\0\0");
  /// s.resize(0);
  /// assert_eq!(unsafe { s.as_view().as_str() }, "");
  /// ```
  pub fn resize(&mut self, len: usize) {
    unsafe {
      self.0.resize(len);
    }
  }

  /// Deallocate the underlying string storage.
  ///
  /// This method is idempotent and can be called multiple times in a row.
  ///
  ///```
  /// let mut s = re2::string::StringWrapper::from_view("asdf".into());
  /// assert_eq!(unsafe { s.as_view().as_str() }, "asdf");
  /// s.clear();
  /// assert_eq!(unsafe { s.as_view().as_str() }, "");
  /// ```
  pub fn clear(&mut self) {
    unsafe {
      self.0.clear();
    }
  }
}

impl ops::Drop for StringWrapper {
  fn drop(&mut self) { self.clear(); }
}