diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/cubeb-pulse/src/backend/intern.rs | |
parent | Initial commit. (diff) | |
download | firefox-esr-upstream.tar.xz firefox-esr-upstream.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/cubeb-pulse/src/backend/intern.rs')
-rw-r--r-- | third_party/rust/cubeb-pulse/src/backend/intern.rs | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/third_party/rust/cubeb-pulse/src/backend/intern.rs b/third_party/rust/cubeb-pulse/src/backend/intern.rs new file mode 100644 index 0000000000..bd20174916 --- /dev/null +++ b/third_party/rust/cubeb-pulse/src/backend/intern.rs @@ -0,0 +1,58 @@ +// Copyright © 2017-2018 Mozilla Foundation +// +// This program is made available under an ISC-style license. See the +// accompanying file LICENSE for details. + +use std::ffi::{CStr, CString}; +use std::os::raw::c_char; + +#[derive(Debug)] +pub struct Intern { + vec: Vec<CString>, +} + +impl Intern { + pub fn new() -> Intern { + Intern { vec: Vec::new() } + } + + pub fn add(&mut self, string: &CStr) -> *const c_char { + fn eq(s1: &CStr, s2: &CStr) -> bool { + s1 == s2 + } + for s in &self.vec { + if eq(s, string) { + return s.as_ptr(); + } + } + + self.vec.push(string.to_owned()); + self.vec.last().unwrap().as_ptr() + } +} + +#[cfg(test)] +mod tests { + use super::Intern; + use std::ffi::CStr; + + #[test] + fn intern() { + fn cstr(str: &[u8]) -> &CStr { + CStr::from_bytes_with_nul(str).unwrap() + } + + let mut intern = Intern::new(); + + let foo_ptr = intern.add(cstr(b"foo\0")); + let bar_ptr = intern.add(cstr(b"bar\0")); + assert!(!foo_ptr.is_null()); + assert!(!bar_ptr.is_null()); + assert!(foo_ptr != bar_ptr); + + assert!(foo_ptr == intern.add(cstr(b"foo\0"))); + assert!(foo_ptr != intern.add(cstr(b"fo\0"))); + assert!(foo_ptr != intern.add(cstr(b"fool\0"))); + assert!(foo_ptr != intern.add(cstr(b"not foo\0"))); + } +} |