Skip to content

Commit fd9d37c

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

File tree

4 files changed

+85
-23
lines changed

4 files changed

+85
-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

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (C) 2020 Free Software Foundation, Inc.
2+
3+
// This file is part of GCC.
4+
5+
// GCC is free software; you can redistribute it and/or modify it under
6+
// the terms of the GNU General Public License as published by the Free
7+
// Software Foundation; either version 3, or (at your option) any later
8+
// version.
9+
10+
// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11+
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
12+
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13+
// for more details.
14+
15+
// You should have received a copy of the GNU General Public License
16+
// along with GCC; see the file COPYING3. If not see
17+
// <http://www.gnu.org/licenses/>.
18+
19+
#include "rust-base62.h"
20+
21+
#include <algorithm>
22+
23+
namespace Rust {
24+
25+
std::string
26+
base62_integer (uint64_t value)
27+
{
28+
const static std::string base_64
29+
= "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@$";
30+
std::string buffer (128, '\0');
31+
size_t idx = 0;
32+
size_t base = 62;
33+
34+
do
35+
{
36+
buffer[idx] = base_64[(value % base)];
37+
idx++;
38+
value = value / base;
39+
}
40+
while (value != 0);
41+
42+
std::reverse (buffer.begin (), buffer.begin () + idx);
43+
return buffer.substr (0, idx);
44+
}
45+
46+
} // namespace Rust
47+
48+
// FIXME: Add unit testing using the selftest framework

gcc/rust/util/rust-base62.h

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (C) 2020 Free Software Foundation, Inc.
2+
3+
// This file is part of GCC.
4+
5+
// GCC is free software; you can redistribute it and/or modify it under
6+
// the terms of the GNU General Public License as published by the Free
7+
// Software Foundation; either version 3, or (at your option) any later
8+
// version.
9+
10+
// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11+
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
12+
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13+
// for more details.
14+
15+
// You should have received a copy of the GNU General Public License
16+
// along with GCC; see the file COPYING3. If not see
17+
// <http://www.gnu.org/licenses/>.
18+
19+
#ifndef RUST_BASE62_H
20+
#define RUST_BASE62_H
21+
22+
#include <string>
23+
24+
namespace Rust {
25+
26+
/**
27+
* Get the Base62 representation of an integer
28+
*/
29+
std::string
30+
base62_integer (uint64_t value);
31+
32+
} // namespace Rust
33+
34+
#endif /* !RUST_BASE62_H */

0 commit comments

Comments
 (0)