string_cache-0.8.0/.gitignore010064400007650000024000000000451272017202200143510ustar0000000000000000/doc Cargo.lock target .cargo/config string_cache-0.8.0/.travis.yml010064400007650000024000000004631355240622100145030ustar0000000000000000sudo: false language: rust rust: - 1.36.0 - nightly - beta - stable os: - linux script: - cargo build - cargo test --all - "cd string-cache-codegen && cargo build && cd .." - "if [ $TRAVIS_RUST_VERSION = nightly ]; then cd integration-tests && cargo test --features unstable && cd ..; fi" string_cache-0.8.0/Cargo.toml.orig010064400007650000024000000017121355240622100152570ustar0000000000000000[package] name = "string_cache" version = "0.8.0" # Also update README.md when making a semver-breaking change authors = [ "The Servo Project Developers" ] description = "A string interning library for Rust, developed as part of the Servo project." license = "MIT / Apache-2.0" repository = "https://github.com/servo/string-cache" documentation = "https://docs.rs/string_cache/" edition = "2018" # Do not `exclude` ./string-cache-codegen because we want to include # ./string-cache-codegen/shared.rs, and `include` is a pain to use # (It has to be exhaustive.) # This means that packages for this crate include some unused files, # but they’re not too big so that shouldn’t be a problem. [lib] name = "string_cache" [dependencies] precomputed-hash = "0.1" lazy_static = "1" serde = "1" phf_shared = "0.8" new_debug_unreachable = "1.0" [[test]] name = "small-stack" harness = false [workspace] members = [ "string-cache-codegen", "integration-tests", ] string_cache-0.8.0/Cargo.toml0000644000000022050000000000000115260ustar00# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] edition = "2018" name = "string_cache" version = "0.8.0" authors = ["The Servo Project Developers"] description = "A string interning library for Rust, developed as part of the Servo project." documentation = "https://docs.rs/string_cache/" license = "MIT / Apache-2.0" repository = "https://github.com/servo/string-cache" [lib] name = "string_cache" [[test]] name = "small-stack" harness = false [dependencies.lazy_static] version = "1" [dependencies.new_debug_unreachable] version = "1.0" [dependencies.phf_shared] version = "0.8" [dependencies.precomputed-hash] version = "0.1" [dependencies.serde] version = "1" string_cache-0.8.0/examples/simple.rs010064400007650000024000000020421355240622100160420ustar0000000000000000 use string_cache::DefaultAtom; fn main() { let mut interned_stuff = Vec::new(); let text = "here is a sentence of text that will be tokenised and interned and some repeated \ tokens is of text and"; for word in text.split_whitespace() { let seen_before = interned_stuff .iter() // We can use impl PartialEq where T is anything string-like to compare to // interned strings to either other interned strings, or actual strings Comparing two // interned strings is very fast (normally a single cpu operation). .filter(|interned_word| interned_word == &word) .count(); if seen_before > 0 { println!(r#"Seen the word "{}" {} times"#, word, seen_before); } else { println!(r#"Not seen the word "{}" before"#, word); } // We use the impl From<(Cow<'a, str>, or &'a str, or String) for Atom to intern a // new string interned_stuff.push(DefaultAtom::from(word)); } } string_cache-0.8.0/LICENSE-APACHE010064400007650000024000000251371272017202200143160ustar0000000000000000 Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. string_cache-0.8.0/LICENSE-MIT010064400007650000024000000020531272017202200140160ustar0000000000000000Copyright (c) 2012-2013 Mozilla Foundation Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. string_cache-0.8.0/README.md010064400007650000024000000026231355240622100136510ustar0000000000000000# string-cache [![Build Status](https://travis-ci.com/servo/string-cache.svg?branch=master)](https://travis-ci.com/servo/string-cache) [Documentation](https://docs.rs/string_cache/) A string interning library for Rust, developed as part of the [Servo](https://github.com/servo/servo) project. ## Simple usage In `Cargo.toml`: ```toml [dependencies] string_cache = "0.8" ``` In `lib.rs`: ```rust extern crate string_cache; use string_cache::DefaultAtom as Atom; ``` ## With static atoms In `Cargo.toml`: ```toml [package] build = "build.rs" [dependencies] string_cache = "0.8" [build-dependencies] string_cache_codegen = "0.5" ``` In `build.rs`: ```rust extern crate string_cache_codegen; use std::env; use std::path::Path; fn main() { string_cache_codegen::AtomType::new("foo::FooAtom", "foo_atom!") .atoms(&["foo", "bar"]) .write_to_file(&Path::new(&env::var("OUT_DIR").unwrap()).join("foo_atom.rs")) .unwrap() } ``` In `lib.rs`: ```rust extern crate string_cache; mod foo { include!(concat!(env!("OUT_DIR"), "/foo_atom.rs")); } ``` The generated code will define a `FooAtom` type and a `foo_atom!` macro. The macro can be used in expression or patterns, with strings listed in `build.rs`. For example: ```rust fn compute_something(input: &foo::FooAtom) -> u32 { match *input { foo_atom!("foo") => 1, foo_atom!("bar") => 2, _ => 3, } } ``` string_cache-0.8.0/src/atom.rs010064400007650000024000000315021355240622100144650ustar0000000000000000// Copyright 2014 The Servo Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution. // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. use crate::dynamic_set::{Entry, DYNAMIC_SET}; use crate::static_sets::StaticAtomSet; use debug_unreachable::debug_unreachable; use phf_shared; use std::borrow::Cow; use std::cmp::Ordering::{self, Equal}; use std::fmt; use std::hash::{Hash, Hasher}; use std::marker::PhantomData; use std::mem; use std::num::NonZeroU64; use std::ops; use std::slice; use std::str; use std::sync::atomic::Ordering::SeqCst; const DYNAMIC_TAG: u8 = 0b_00; const INLINE_TAG: u8 = 0b_01; // len in upper nybble const STATIC_TAG: u8 = 0b_10; const TAG_MASK: u64 = 0b_11; const LEN_OFFSET: u64 = 4; const LEN_MASK: u64 = 0xF0; const MAX_INLINE_LEN: usize = 7; const STATIC_SHIFT_BITS: usize = 32; /// Represents a string that has been interned. /// /// While the type definition for `Atom` indicates that it generic on a particular /// implementation of an atom set, you don't need to worry about this. Atoms can be static /// and come from a `StaticAtomSet` generated by the `string_cache_codegen` crate, or they /// can be dynamic and created by you on an `EmptyStaticAtomSet`. /// /// `Atom` implements `Clone` but not `Copy`, since internally atoms are reference-counted; /// this means that you may need to `.clone()` an atom to keep copies to it in different /// places, or when passing it to a function that takes an `Atom` rather than an `&Atom`. /// /// ## Creating an atom at runtime /// /// If you use `string_cache_codegen` to generate a precomputed list of atoms, your code /// may then do something like read data from somewhere and extract tokens that need to be /// compared to the atoms. In this case, you can use `Atom::from(&str)` or /// `Atom::from(String)`. These create a reference-counted atom which will be /// automatically freed when all references to it are dropped. /// /// This means that your application can safely have a loop which tokenizes data, creates /// atoms from the tokens, and compares the atoms to a predefined set of keywords, without /// running the risk of arbitrary memory consumption from creating large numbers of atoms — /// as long as your application does not store clones of the atoms it creates along the /// way. /// /// For example, the following is safe and will not consume arbitrary amounts of memory: /// /// ```ignore /// let untrusted_data = "large amounts of text ..."; /// /// for token in untrusted_data.split_whitespace() { /// let atom = Atom::from(token); // interns the string /// /// if atom == Atom::from("keyword") { /// // handle that keyword /// } else if atom == Atom::from("another_keyword") { /// // handle that keyword /// } else { /// println!("unknown keyword"); /// } /// } // atom is dropped here, so it is not kept around in memory /// ``` #[derive(PartialEq, Eq)] // NOTE: Deriving PartialEq requires that a given string must always be interned the same way. pub struct Atom { unsafe_data: NonZeroU64, phantom: PhantomData, } // FIXME: bound removed from the struct definition before of this error for pack_static: // "error[E0723]: trait bounds other than `Sized` on const fn parameters are unstable" // https://github.com/rust-lang/rust/issues/57563 impl Atom { /// For the atom!() macros #[inline(always)] #[doc(hidden)] pub const fn pack_static(n: u32) -> Self { Self { unsafe_data: unsafe { // STATIC_TAG ensures this is non-zero NonZeroU64::new_unchecked((STATIC_TAG as u64) | ((n as u64) << STATIC_SHIFT_BITS)) }, phantom: PhantomData, } } fn tag(&self) -> u8 { (self.unsafe_data.get() & TAG_MASK) as u8 } } impl Atom { /// Return the internal repersentation. For testing. #[doc(hidden)] pub fn unsafe_data(&self) -> u64 { self.unsafe_data.get() } /// Return true if this is a static Atom. For testing. #[doc(hidden)] pub fn is_static(&self) -> bool { self.tag() == STATIC_TAG } /// Return true if this is a dynamic Atom. For testing. #[doc(hidden)] pub fn is_dynamic(&self) -> bool { self.tag() == DYNAMIC_TAG } /// Return true if this is an inline Atom. For testing. #[doc(hidden)] pub fn is_inline(&self) -> bool { self.tag() == INLINE_TAG } fn static_index(&self) -> u64 { self.unsafe_data.get() >> STATIC_SHIFT_BITS } /// Get the hash of the string as it is stored in the set. pub fn get_hash(&self) -> u32 { match self.tag() { DYNAMIC_TAG => { let entry = self.unsafe_data.get() as *const Entry; unsafe { (*entry).hash } } STATIC_TAG => Static::get().hashes[self.static_index() as usize], INLINE_TAG => { let data = self.unsafe_data.get(); // This may or may not be great... ((data >> 32) ^ data) as u32 } _ => unsafe { debug_unreachable!() }, } } } impl Default for Atom { #[inline] fn default() -> Self { Atom::pack_static(Static::empty_string_index()) } } impl Hash for Atom { #[inline] fn hash(&self, state: &mut H) where H: Hasher, { state.write_u32(self.get_hash()) } } impl<'a, Static: StaticAtomSet> From> for Atom { fn from(string_to_add: Cow<'a, str>) -> Self { let static_set = Static::get(); let hash = phf_shared::hash(&*string_to_add, &static_set.key); let index = phf_shared::get_index(&hash, static_set.disps, static_set.atoms.len()); if static_set.atoms[index as usize] == string_to_add { Self::pack_static(index) } else { let len = string_to_add.len(); if len <= MAX_INLINE_LEN { let mut data: u64 = (INLINE_TAG as u64) | ((len as u64) << LEN_OFFSET); { let dest = inline_atom_slice_mut(&mut data); dest[..len].copy_from_slice(string_to_add.as_bytes()) } Atom { // INLINE_TAG ensures this is never zero unsafe_data: unsafe { NonZeroU64::new_unchecked(data) }, phantom: PhantomData, } } else { let ptr: std::ptr::NonNull = DYNAMIC_SET.lock().unwrap().insert(string_to_add, hash.g); let data = ptr.as_ptr() as u64; debug_assert!(0 == data & TAG_MASK); Atom { // The address of a ptr::NonNull is non-zero unsafe_data: unsafe { NonZeroU64::new_unchecked(data) }, phantom: PhantomData, } } } } } impl Clone for Atom { #[inline(always)] fn clone(&self) -> Self { if self.tag() == DYNAMIC_TAG { let entry = self.unsafe_data.get() as *const Entry; unsafe { &*entry }.ref_count.fetch_add(1, SeqCst); } Atom { ..*self } } } impl Drop for Atom { #[inline] fn drop(&mut self) { if self.tag() == DYNAMIC_TAG { let entry = self.unsafe_data.get() as *const Entry; if unsafe { &*entry }.ref_count.fetch_sub(1, SeqCst) == 1 { drop_slow(self) } } // Out of line to guide inlining. fn drop_slow(this: &mut Atom) { DYNAMIC_SET .lock() .unwrap() .remove(this.unsafe_data.get() as *mut Entry); } } } impl ops::Deref for Atom { type Target = str; #[inline] fn deref(&self) -> &str { unsafe { match self.tag() { DYNAMIC_TAG => { let entry = self.unsafe_data.get() as *const Entry; &(*entry).string } INLINE_TAG => { let len = (self.unsafe_data() & LEN_MASK) >> LEN_OFFSET; let src = inline_atom_slice(&self.unsafe_data); str::from_utf8_unchecked(&src[..(len as usize)]) } STATIC_TAG => Static::get().atoms[self.static_index() as usize], _ => debug_unreachable!(), } } } } impl fmt::Debug for Atom { #[inline] fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let ty_str = unsafe { match self.tag() { DYNAMIC_TAG => "dynamic", INLINE_TAG => "inline", STATIC_TAG => "static", _ => debug_unreachable!(), } }; write!(f, "Atom('{}' type={})", &*self, ty_str) } } impl PartialOrd for Atom { #[inline] fn partial_cmp(&self, other: &Self) -> Option { if self.unsafe_data == other.unsafe_data { return Some(Equal); } self.as_ref().partial_cmp(other.as_ref()) } } impl Ord for Atom { #[inline] fn cmp(&self, other: &Self) -> Ordering { if self.unsafe_data == other.unsafe_data { return Equal; } self.as_ref().cmp(other.as_ref()) } } // AsciiExt requires mutating methods, so we just implement the non-mutating ones. // We don't need to implement is_ascii because there's no performance improvement // over the one from &str. impl Atom { fn from_mutated_str(s: &str, f: F) -> Self { let mut buffer = mem::MaybeUninit::<[u8; 64]>::uninit(); let buffer = unsafe { &mut *buffer.as_mut_ptr() }; if let Some(buffer_prefix) = buffer.get_mut(..s.len()) { buffer_prefix.copy_from_slice(s.as_bytes()); let as_str = unsafe { ::std::str::from_utf8_unchecked_mut(buffer_prefix) }; f(as_str); Atom::from(&*as_str) } else { let mut string = s.to_owned(); f(&mut string); Atom::from(string) } } /// Like [`to_ascii_uppercase`]. /// /// [`to_ascii_uppercase`]: https://doc.rust-lang.org/std/ascii/trait.AsciiExt.html#tymethod.to_ascii_uppercase pub fn to_ascii_uppercase(&self) -> Self { for (i, b) in self.bytes().enumerate() { if let b'a'..=b'z' = b { return Atom::from_mutated_str(self, |s| s[i..].make_ascii_uppercase()); } } self.clone() } /// Like [`to_ascii_lowercase`]. /// /// [`to_ascii_lowercase`]: https://doc.rust-lang.org/std/ascii/trait.AsciiExt.html#tymethod.to_ascii_lowercase pub fn to_ascii_lowercase(&self) -> Self { for (i, b) in self.bytes().enumerate() { if let b'A'..=b'Z' = b { return Atom::from_mutated_str(self, |s| s[i..].make_ascii_lowercase()); } } self.clone() } /// Like [`eq_ignore_ascii_case`]. /// /// [`eq_ignore_ascii_case`]: https://doc.rust-lang.org/std/ascii/trait.AsciiExt.html#tymethod.eq_ignore_ascii_case pub fn eq_ignore_ascii_case(&self, other: &Self) -> bool { (self == other) || self.eq_str_ignore_ascii_case(&**other) } /// Like [`eq_ignore_ascii_case`], but takes an unhashed string as `other`. /// /// [`eq_ignore_ascii_case`]: https://doc.rust-lang.org/std/ascii/trait.AsciiExt.html#tymethod.eq_ignore_ascii_case pub fn eq_str_ignore_ascii_case(&self, other: &str) -> bool { (&**self).eq_ignore_ascii_case(other) } } #[inline(always)] fn inline_atom_slice(x: &NonZeroU64) -> &[u8] { unsafe { let x: *const NonZeroU64 = x; let mut data = x as *const u8; // All except the lowest byte, which is first in little-endian, last in big-endian. if cfg!(target_endian = "little") { data = data.offset(1); } let len = 7; slice::from_raw_parts(data, len) } } #[inline(always)] fn inline_atom_slice_mut(x: &mut u64) -> &mut [u8] { unsafe { let x: *mut u64 = x; let mut data = x as *mut u8; // All except the lowest byte, which is first in little-endian, last in big-endian. if cfg!(target_endian = "little") { data = data.offset(1); } let len = 7; slice::from_raw_parts_mut(data, len) } } string_cache-0.8.0/src/dynamic_set.rs010064400007650000024000000075671355240622100160420ustar0000000000000000// Copyright 2014 The Servo Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution. // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. use lazy_static::lazy_static; use std::borrow::Cow; use std::mem; use std::ptr::NonNull; use std::sync::atomic::AtomicIsize; use std::sync::atomic::Ordering::SeqCst; use std::sync::Mutex; const NB_BUCKETS: usize = 1 << 12; // 4096 const BUCKET_MASK: u32 = (1 << 12) - 1; pub(crate) struct Set { buckets: Box<[Option>; NB_BUCKETS]>, } pub(crate) struct Entry { pub(crate) string: Box, pub(crate) hash: u32, pub(crate) ref_count: AtomicIsize, next_in_bucket: Option>, } // Addresses are a multiples of this, // and therefore have have TAG_MASK bits unset, available for tagging. pub(crate) const ENTRY_ALIGNMENT: usize = 4; #[test] fn entry_alignment_is_sufficient() { assert!(mem::align_of::() >= ENTRY_ALIGNMENT); } lazy_static! { pub(crate) static ref DYNAMIC_SET: Mutex = Mutex::new({ type T = Option>; let _static_assert_size_eq = std::mem::transmute::; let vec = std::mem::ManuallyDrop::new(vec![0_usize; NB_BUCKETS]); Set { buckets: unsafe { Box::from_raw(vec.as_ptr() as *mut [T; NB_BUCKETS]) }, } }); } impl Set { pub(crate) fn insert(&mut self, string: Cow, hash: u32) -> NonNull { let bucket_index = (hash & BUCKET_MASK) as usize; { let mut ptr: Option<&mut Box> = self.buckets[bucket_index].as_mut(); while let Some(entry) = ptr.take() { if entry.hash == hash && &*entry.string == &*string { if entry.ref_count.fetch_add(1, SeqCst) > 0 { return NonNull::from(&mut **entry); } // Uh-oh. The pointer's reference count was zero, which means someone may try // to free it. (Naive attempts to defend against this, for example having the // destructor check to see whether the reference count is indeed zero, don't // work due to ABA.) Thus we need to temporarily add a duplicate string to the // list. entry.ref_count.fetch_sub(1, SeqCst); break; } ptr = entry.next_in_bucket.as_mut(); } } debug_assert!(mem::align_of::() >= ENTRY_ALIGNMENT); let string = string.into_owned(); let mut entry = Box::new(Entry { next_in_bucket: self.buckets[bucket_index].take(), hash, ref_count: AtomicIsize::new(1), string: string.into_boxed_str(), }); let ptr = NonNull::from(&mut *entry); self.buckets[bucket_index] = Some(entry); ptr } pub(crate) fn remove(&mut self, ptr: *mut Entry) { let bucket_index = { let value: &Entry = unsafe { &*ptr }; debug_assert!(value.ref_count.load(SeqCst) == 0); (value.hash & BUCKET_MASK) as usize }; let mut current: &mut Option> = &mut self.buckets[bucket_index]; loop { let entry_ptr: *mut Entry = match current.as_mut() { Some(entry) => &mut **entry, None => break, }; if entry_ptr == ptr { mem::drop(mem::replace(current, unsafe { (*entry_ptr).next_in_bucket.take() })); break; } current = unsafe { &mut (*entry_ptr).next_in_bucket }; } } } string_cache-0.8.0/src/lib.rs010064400007650000024000000075531355240622100143040ustar0000000000000000// Copyright 2014 The Servo Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution. // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. //! //! A library for interning things that are `AsRef`. //! //! Some strings may be interned at compile time using the `string-cache-codegen` crate, or the //! `EmptyStaticAtomSet` may be used that has no compile-time interned strings. An `Atom` is an //! interned string for a given set (either `EmptyStaticAtomSet` or a generated `StaticAtomSet`). //! //! Generated `Atom`s will have assocated macros to intern static strings at compile-time. //! //! # Examples //! //! Here are two examples, one with compile-time `Atom`s, and one without. //! //! ## With compile-time atoms //! //! In `Cargo.toml`: //! ```toml //! [dependencies] //! string_cache = "0.8" //! //! [dev-dependencies] //! string_cache_codegen = "0.5" //! ``` //! //! In `build.rs`: //! //! ```ignore //! extern crate string_cache_codegen; //! //! use std::env; //! use std::path::Path; //! //! fn main() { //! string_cache_codegen::AtomType::new("foo::FooAtom", "foo_atom!") //! .atoms(&["foo", "bar"]) //! .write_to_file(&Path::new(&env::var("OUT_DIR").unwrap()).join("foo_atom.rs")) //! .unwrap() //! } //! ``` //! //! In `lib.rs`: //! //! ```ignore //! extern crate string_cache; //! //! mod foo { //! include!(concat!(env!("OUT_DIR"), "/foo_atom.rs")); //! } //! //! fn use_the_atom(t: &str) { //! match *t { //! foo_atom!("foo") => println!("Found foo!"), //! foo_atom!("bar") => println!("Found bar!"), //! // foo_atom!("baz") => println!("Found baz!"), - would be a compile time error //! _ => { //! println!("String not interned"); //! // We can intern strings at runtime as well //! foo::FooAtom::from(t) //! } //! } //! } //! ``` //! //! ## No compile-time atoms //! //! ``` //! # extern crate string_cache; //! use string_cache::DefaultAtom; //! //! # fn main() { //! let mut interned_stuff = Vec::new(); //! let text = "here is a sentence of text that will be tokenised and //! interned and some repeated tokens is of text and"; //! for word in text.split_whitespace() { //! let seen_before = interned_stuff.iter() //! // We can use impl PartialEq where T is anything string-like //! // to compare to interned strings to either other interned strings, //! // or actual strings Comparing two interned strings is very fast //! // (normally a single cpu operation). //! .filter(|interned_word| interned_word == &word) //! .count(); //! if seen_before > 0 { //! println!(r#"Seen the word "{}" {} times"#, word, seen_before); //! } else { //! println!(r#"Not seen the word "{}" before"#, word); //! } //! // We use the impl From<(Cow<'a, str>, or &'a str, or String)> for //! // Atom to intern a new string. //! interned_stuff.push(DefaultAtom::from(word)); //! } //! # } //! ``` //! #![cfg_attr(test, deny(warnings))] mod atom; mod dynamic_set; mod static_sets; mod trivial_impls; pub use atom::Atom; pub use static_sets::{EmptyStaticAtomSet, PhfStrSet, StaticAtomSet}; /// Use this if you don’t care about static atoms. pub type DefaultAtom = Atom; // Some minor tests of internal layout here. // See ../integration-tests for much more. /// Guard against accidental changes to the sizes of things. #[test] fn assert_sizes() { use std::mem::size_of; assert_eq!(size_of::(), 8); assert_eq!(size_of::>(), size_of::(),); } string_cache-0.8.0/src/static_sets.rs010064400007650000024000000043551355240622100160600ustar0000000000000000// Copyright 2014 The Servo Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution. // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. /// A static `PhfStrSet` /// /// This trait is implemented by static sets of interned strings generated using /// `string_cache_codegen`, and `EmptyStaticAtomSet` for when strings will be added dynamically. /// /// It is used by the methods of [`Atom`] to check if a string is present in the static set. /// /// [`Atom`]: struct.Atom.html pub trait StaticAtomSet: Ord { /// Get the location of the static string set in the binary. fn get() -> &'static PhfStrSet; /// Get the index of the empty string, which is in every set and is used for `Atom::default`. fn empty_string_index() -> u32; } /// A string set created using a [perfect hash function], specifically /// [Hash, Displace and Compress]. /// /// See the CHD document for the meaning of the struct fields. /// /// [perfect hash function]: https://en.wikipedia.org/wiki/Perfect_hash_function /// [Hash, Displace and Compress]: http://cmph.sourceforge.net/papers/esa09.pdf pub struct PhfStrSet { #[doc(hidden)] pub key: u64, #[doc(hidden)] pub disps: &'static [(u32, u32)], #[doc(hidden)] pub atoms: &'static [&'static str], #[doc(hidden)] pub hashes: &'static [u32], } /// An empty static atom set for when only dynamic strings will be added #[derive(PartialEq, Eq, PartialOrd, Ord)] pub struct EmptyStaticAtomSet; impl StaticAtomSet for EmptyStaticAtomSet { fn get() -> &'static PhfStrSet { // The name is a lie: this set is not empty (it contains the empty string) // but that’s only to avoid divisions by zero in rust-phf. static SET: PhfStrSet = PhfStrSet { key: 0, disps: &[(0, 0)], atoms: &[""], // "" SipHash'd, and xored with u64_hash_to_u32. hashes: &[0x3ddddef3], }; &SET } fn empty_string_index() -> u32 { 0 } } string_cache-0.8.0/src/trivial_impls.rs010064400007650000024000000046641355240622100164140ustar0000000000000000// Copyright 2014 The Servo Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution. // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. use crate::{Atom, StaticAtomSet}; use serde::{Deserialize, Deserializer, Serialize, Serializer}; use std::borrow::Cow; use std::fmt; impl ::precomputed_hash::PrecomputedHash for Atom { fn precomputed_hash(&self) -> u32 { self.get_hash() } } impl<'a, Static: StaticAtomSet> From<&'a Atom> for Atom { fn from(atom: &'a Self) -> Self { atom.clone() } } impl PartialEq for Atom { fn eq(&self, other: &str) -> bool { &self[..] == other } } impl PartialEq> for str { fn eq(&self, other: &Atom) -> bool { self == &other[..] } } impl PartialEq for Atom { fn eq(&self, other: &String) -> bool { &self[..] == &other[..] } } impl<'a, Static: StaticAtomSet> From<&'a str> for Atom { #[inline] fn from(string_to_add: &str) -> Self { Atom::from(Cow::Borrowed(string_to_add)) } } impl From for Atom { #[inline] fn from(string_to_add: String) -> Self { Atom::from(Cow::Owned(string_to_add)) } } impl fmt::Display for Atom { #[inline] fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { ::fmt(self, f) } } impl AsRef for Atom { fn as_ref(&self) -> &str { &self } } impl Serialize for Atom { fn serialize(&self, serializer: S) -> Result where S: Serializer, { let string: &str = self.as_ref(); string.serialize(serializer) } } impl<'a, Static: StaticAtomSet> Deserialize<'a> for Atom { fn deserialize(deserializer: D) -> Result where D: Deserializer<'a>, { let string: String = Deserialize::deserialize(deserializer)?; Ok(Atom::from(string)) } } string_cache-0.8.0/tests/small-stack.rs010064400007650000024000000010651355240622100163140ustar0000000000000000// Regression test for https://github.com/servo/html5ever/issues/393 // // Create a dynamic atom − causing initialization of the golbal hash map − // in a thread that has a small stack. // // This is a separate test program rather than a `#[test] fn` among others // to make sure that nothing else has already initialized the map in this process. fn main() { std::thread::Builder::new() .stack_size(50_000) .spawn(|| { string_cache::DefaultAtom::from("12345678"); }) .unwrap() .join() .unwrap() } string_cache-0.8.0/.cargo_vcs_info.json0000644000000001120000000000000135230ustar00{ "git": { "sha1": "23e923935cafb644d0e4b69743ae79d4822e7858" } } string_cache-0.8.0/Cargo.lock0000644000000044070000000000000115110ustar00# This file is automatically @generated by Cargo. # It is not intended for manual editing. [[package]] name = "lazy_static" version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "new_debug_unreachable" version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "phf_shared" version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "siphasher 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "precomputed-hash" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "serde" version = "1.0.101" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "siphasher" version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "string_cache" version = "0.8.0" dependencies = [ "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "new_debug_unreachable 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", "phf_shared 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "precomputed-hash 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.101 (registry+https://github.com/rust-lang/crates.io-index)", ] [metadata] "checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" "checksum new_debug_unreachable 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "f40f005c60db6e03bae699e414c58bf9aa7ea02a2d0b9bfbcf19286cc4c82b30" "checksum phf_shared 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c00cf8b9eafe68dde5e9eaa2cef8ee84a9336a47d566ec55ca16589633b65af7" "checksum precomputed-hash 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c" "checksum serde 1.0.101 (registry+https://github.com/rust-lang/crates.io-index)" = "9796c9b7ba2ffe7a9ce53c2287dfc48080f4b2b362fcc245a259b3a7201119dd" "checksum siphasher 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "83da420ee8d1a89e640d0948c646c1c088758d3a3c538f943bfa97bdac17929d"