From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7905) id 7751A3873873; Tue, 13 Dec 2022 13:18:43 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7751A3873873 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1670937523; bh=VKY8tI4M+Rf18fgFGDxvyHJGCzdvPBlJpC+ab/Kh86A=; h=From:To:Subject:Date:From; b=HDkDiKt+v8PvlNJNuEW85TOWPWGM9/6p2hlVWBRXTaVLxwBc3F7ByxZLdqc0CgwxP lbFLIifEiZKx5C6rbIMIzPw8UX/SiaXaV2Da8Qf6L/uNtq9BOnnW2Hid8MwkTLsRDa Nd0BJT2I7A9z7z52/vT2I7CPzY/sVAVx9z0HVXDU= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Arthur Cohen To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-4653] gccrs: Add Base62 implementation X-Act-Checkin: gcc X-Git-Author: Arthur Cohen X-Git-Refname: refs/heads/master X-Git-Oldrev: 15b0278905ed80413867ad78868a597dd7227170 X-Git-Newrev: eb10bc5225e03c32175b32c4778e937e64f7ddaa Message-Id: <20221213131843.7751A3873873@sourceware.org> Date: Tue, 13 Dec 2022 13:18:43 +0000 (GMT) List-Id: https://gcc.gnu.org/g:eb10bc5225e03c32175b32c4778e937e64f7ddaa commit r13-4653-geb10bc5225e03c32175b32c4778e937e64f7ddaa Author: Arthur Cohen Date: Tue Aug 23 16:26:01 2022 +0100 gccrs: Add Base62 implementation Used for V0 symbol mangling scheme which is still in development. gcc/rust/ * util/rust-base62.cc: New. * util/rust-base62.h: New. Diff: --- gcc/rust/util/rust-base62.cc | 46 ++++++++++++++++++++++++++++++++++++++++++++ gcc/rust/util/rust-base62.h | 34 ++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) diff --git a/gcc/rust/util/rust-base62.cc b/gcc/rust/util/rust-base62.cc new file mode 100644 index 00000000000..bdab23338c3 --- /dev/null +++ b/gcc/rust/util/rust-base62.cc @@ -0,0 +1,46 @@ +// Copyright (C) 2020-2022 Free Software Foundation, Inc. + +// This file is part of GCC. + +// GCC is free software; you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free +// Software Foundation; either version 3, or (at your option) any later +// version. + +// GCC is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. + +// You should have received a copy of the GNU General Public License +// along with GCC; see the file COPYING3. If not see +// . + +#include "rust-base62.h" + +namespace Rust { + +std::string +base62_integer (uint64_t value) +{ + const static std::string base_64 + = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@$"; + std::string buffer (128, '\0'); + size_t idx = 0; + size_t base = 62; + + do + { + buffer[idx] = base_64[(value % base)]; + idx++; + value = value / base; + } + while (value != 0); + + std::reverse (buffer.begin (), buffer.begin () + idx); + return buffer.substr (0, idx); +} + +} // namespace Rust + +// FIXME: Add unit testing using the selftest framework diff --git a/gcc/rust/util/rust-base62.h b/gcc/rust/util/rust-base62.h new file mode 100644 index 00000000000..fa610d3e5a4 --- /dev/null +++ b/gcc/rust/util/rust-base62.h @@ -0,0 +1,34 @@ +// Copyright (C) 2020-2022 Free Software Foundation, Inc. + +// This file is part of GCC. + +// GCC is free software; you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free +// Software Foundation; either version 3, or (at your option) any later +// version. + +// GCC is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. + +// You should have received a copy of the GNU General Public License +// along with GCC; see the file COPYING3. If not see +// . + +#ifndef RUST_BASE62_H +#define RUST_BASE62_H + +#include "rust-system.h" + +namespace Rust { + +/** + * Get the Base62 representation of an integer + */ +std::string +base62_integer (uint64_t value); + +} // namespace Rust + +#endif /* !RUST_BASE62_H */