Skip to content

Commit f425f31

Browse files
committed
util: Add rust-base62 implementation from rustc
1 parent 35f6d0f commit f425f31

File tree

4 files changed

+49
-23
lines changed

4 files changed

+49
-23
lines changed

gcc/rust/Make-lang.in

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ GRS_OBJS = \
8888
rust/rust-lint-marklive.o \
8989
rust/rust-hir-type-check-path.o \
9090
rust/rust-compile-intrinsic.o \
91+
rust/rust-base62.o \
9192
$(END)
9293
# removed object files from here
9394

gcc/rust/backend/rust-mangle.cc

+2-23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "rust-mangle.h"
22
#include "fnv-hash.h"
3+
#include "rust-base62.h"
34
#include <algorithm>
45

56
// FIXME: Rename those to legacy_*
@@ -155,28 +156,6 @@ v0_simple_type_prefix (const TyTy::BaseType *ty)
155156
gcc_unreachable ();
156157
}
157158

158-
// FIXME: Is this present somewhere in libbiberty already?
159-
static std::string
160-
v0_base62_integer (uint64_t x)
161-
{
162-
const static std::string base_64
163-
= "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@$";
164-
std::string buffer (128, '\0');
165-
size_t idx = 0;
166-
size_t base = 62;
167-
168-
do
169-
{
170-
buffer[idx] = base_64[(x % base)];
171-
idx++;
172-
x = x / base;
173-
}
174-
while (x != 0);
175-
176-
std::reverse (buffer.begin (), buffer.begin () + idx);
177-
return buffer.substr (0, idx);
178-
}
179-
180159
// Add an underscore-terminated base62 integer to the mangling string.
181160
// This corresponds to the `<base-62-number>` grammar in the v0 mangling RFC:
182161
// - 0 is encoded as "_"
@@ -185,7 +164,7 @@ static void
185164
v0_add_integer_62 (std::string &mangled, uint64_t x)
186165
{
187166
if (x > 0)
188-
mangled.append (v0_base62_integer (x - 1));
167+
mangled.append (base62_integer (x - 1));
189168

190169
mangled.append ("_");
191170
}

gcc/rust/util/rust-base62.cc

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include "rust-base62.h"
2+
3+
#include <algorithm>
4+
5+
namespace Rust {
6+
7+
std::string
8+
base62_integer (uint64_t value)
9+
{
10+
const static std::string base_64
11+
= "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@$";
12+
std::string buffer (128, '\0');
13+
size_t idx = 0;
14+
size_t base = 62;
15+
16+
do
17+
{
18+
buffer[idx] = base_64[(value % base)];
19+
idx++;
20+
value = value / base;
21+
}
22+
while (value != 0);
23+
24+
std::reverse (buffer.begin (), buffer.begin () + idx);
25+
return buffer.substr (0, idx);
26+
}
27+
28+
} // namespace Rust
29+
30+
// FIXME: Add unit testing using the selftest framework

gcc/rust/util/rust-base62.h

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef RUST_BASE62_H
2+
#define RUST_BASE62_H
3+
4+
#include <string>
5+
6+
namespace Rust {
7+
8+
/**
9+
* Get the Base62 representation of an integer
10+
*/
11+
std::string
12+
base62_integer (uint64_t value);
13+
14+
} // namespace Rust
15+
16+
#endif /* !RUST_BASE62_H */

0 commit comments

Comments
 (0)