Fuzion Logo
fuzion-lang.dev — The Fuzion Language Portal
JavaScript seems to be disabled. Functionality is limited.

encodings/base32hex.fz


# This file is part of the Fuzion language implementation.
#
# The Fuzion language implementation 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, version 3 of the License.
#
# The Fuzion language implementation 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 The
# Fuzion language implementation.  If not, see <https://www.gnu.org/licenses/>.


# -----------------------------------------------------------------------
#
#  Tokiwa Software GmbH, Germany
#
#  Source code of Fuzion standard library feature base32hex
#
# -----------------------------------------------------------------------

# Base32hex encoding and decoding as defined in RFC 4648
# https://datatracker.ietf.org/doc/html/rfc4648#section-7
#
public base32hex : base32 "0123456789ABCDEFGHIJKLMNOPQRSTUV".utf8.as_array "base32hex" is

  # decode a valid base32hex character to 5 bits
  #
  module redef quintet_bits(n u8) =>
    if 65 <= n <= 86        # case A-V
      n.as_u64 - 55
    else                    # case 0-9
      n.as_u64 - 48


  # checks if a character is valid in the encoding
  #
  module redef is_valid(c u8) =>
    (65 <= c <= 86) || (48 <= c <= 57)

last changed: 2025-06-17