]>
git.proxmox.com Git - rustc.git/blob - vendor/object-0.20.0/src/write/string.rs
1 use indexmap
::IndexSet
;
4 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
5 pub(crate) struct StringId(usize);
7 #[derive(Debug, Default)]
8 pub(crate) struct StringTable
<'a
> {
9 strings
: IndexSet
<&'a
[u8]>,
13 impl<'a
> StringTable
<'a
> {
14 /// Add a string to the string table.
16 /// Panics if the string table has already been written, or
17 /// if the string contains a null byte.
18 pub fn add(&mut self, string
: &'a
[u8]) -> StringId
{
19 assert
!(self.offsets
.is_empty());
20 assert
!(!string
.contains(&0));
21 let id
= self.strings
.insert_full(string
).0;
25 /// Return the offset of the given string.
27 /// Panics if the string table has not been written, or
28 /// if the string is not in the string table.
29 pub fn get_offset(&self, id
: StringId
) -> usize {
33 /// Append the string table to the given `Vec`, and
34 /// calculate the list of string offsets.
36 /// `base` is the initial string table offset. For example,
37 /// this should be 1 for ELF, to account for the initial
38 /// null byte (which must have been written by the caller).
39 pub fn write(&mut self, base
: usize, w
: &mut Vec
<u8>) {
40 assert
!(self.offsets
.is_empty());
42 let mut ids
: Vec
<_
> = (0..self.strings
.len()).collect();
43 sort(&mut ids
, 1, &self.strings
);
45 self.offsets
= vec
![0; ids
.len()];
46 let mut offset
= base
;
47 let mut previous
= &[][..];
49 let string
= self.strings
.get_index(id
).unwrap();
50 if previous
.ends_with(string
) {
51 self.offsets
[id
] = offset
- string
.len() - 1;
53 self.offsets
[id
] = offset
;
54 w
.extend_from_slice(string
);
56 offset
+= string
.len() + 1;
63 // Multi-key quicksort.
65 // Ordering is such that if a string is a suffix of at least one other string,
66 // then it is placed immediately after one of those strings. That is:
67 // - comparison starts at the end of the string
68 // - shorter strings come later
70 // Based on the implementation in LLVM.
71 fn sort(mut ids
: &mut [usize], mut pos
: usize, strings
: &IndexSet
<&[u8]>) {
77 let pivot
= byte(ids
[0], pos
, strings
);
79 let mut upper
= ids
.len();
82 let b
= byte(ids
[i
], pos
, strings
);
95 sort(&mut ids
[..lower
], pos
, strings
);
96 sort(&mut ids
[upper
..], pos
, strings
);
101 ids
= &mut ids
[lower
..upper
];
106 fn byte(id
: usize, pos
: usize, strings
: &IndexSet
<&[u8]>) -> u8 {
107 let string
= strings
.get_index(id
).unwrap();
108 let len
= string
.len();
112 // We know the strings don't contain null bytes.
123 let mut table
= StringTable
::default();
124 let id0
= table
.add(b
"");
125 let id1
= table
.add(b
"foo");
126 let id2
= table
.add(b
"bar");
127 let id3
= table
.add(b
"foobar");
129 let mut data
= Vec
::new();
131 table
.write(1, &mut data
);
132 assert_eq
!(data
, b
"\0foobar\0foo\0");
134 assert_eq
!(table
.get_offset(id0
), 11);
135 assert_eq
!(table
.get_offset(id1
), 8);
136 assert_eq
!(table
.get_offset(id2
), 4);
137 assert_eq
!(table
.get_offset(id3
), 1);