Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Algol68
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C with Coccinelle
C++ with Coccinelle
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Mojo
Nim
Numba
Nix
Objective-C
Objective-C++
OCaml
Odin
OpenCL C
Pascal
Pony
PTX
Python
Racket
Raku
Ruby
Rust
Sail
Snowball
Scala
Slang
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
Triton
TypeScript Native
V
Vala
Visual Basic
Vyper
WASM
Yul (Solidity IR)
Zig
Javascript
GIMPLE
Ygen
sway
c++ source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
6502-c++ 11.1.0
ARM GCC 10.2.0
ARM GCC 10.3.0
ARM GCC 10.4.0
ARM GCC 10.5.0
ARM GCC 11.1.0
ARM GCC 11.2.0
ARM GCC 11.3.0
ARM GCC 11.4.0
ARM GCC 12.1.0
ARM GCC 12.2.0
ARM GCC 12.3.0
ARM GCC 12.4.0
ARM GCC 12.5.0
ARM GCC 13.1.0
ARM GCC 13.2.0
ARM GCC 13.2.0 (unknown-eabi)
ARM GCC 13.3.0
ARM GCC 13.3.0 (unknown-eabi)
ARM GCC 13.4.0
ARM GCC 13.4.0 (unknown-eabi)
ARM GCC 14.1.0
ARM GCC 14.1.0 (unknown-eabi)
ARM GCC 14.2.0
ARM GCC 14.2.0 (unknown-eabi)
ARM GCC 14.3.0
ARM GCC 14.3.0 (unknown-eabi)
ARM GCC 15.1.0
ARM GCC 15.1.0 (unknown-eabi)
ARM GCC 15.2.0
ARM GCC 15.2.0 (unknown-eabi)
ARM GCC 4.5.4
ARM GCC 4.6.4
ARM GCC 5.4
ARM GCC 6.3.0
ARM GCC 6.4.0
ARM GCC 7.3.0
ARM GCC 7.5.0
ARM GCC 8.2.0
ARM GCC 8.5.0
ARM GCC 9.3.0
ARM GCC 9.4.0
ARM GCC 9.5.0
ARM GCC trunk
ARM gcc 10.2.1 (none)
ARM gcc 10.3.1 (2021.07 none)
ARM gcc 10.3.1 (2021.10 none)
ARM gcc 11.2.1 (none)
ARM gcc 5.4.1 (none)
ARM gcc 7.2.1 (none)
ARM gcc 8.2 (WinCE)
ARM gcc 8.3.1 (none)
ARM gcc 9.2.1 (none)
ARM msvc v19.0 (ex-WINE)
ARM msvc v19.10 (ex-WINE)
ARM msvc v19.14 (ex-WINE)
ARM64 Morello gcc 10.1 Alpha 2
ARM64 gcc 10.2
ARM64 gcc 10.3
ARM64 gcc 10.4
ARM64 gcc 10.5.0
ARM64 gcc 11.1
ARM64 gcc 11.2
ARM64 gcc 11.3
ARM64 gcc 11.4.0
ARM64 gcc 12.1
ARM64 gcc 12.2.0
ARM64 gcc 12.3.0
ARM64 gcc 12.4.0
ARM64 gcc 12.5.0
ARM64 gcc 13.1.0
ARM64 gcc 13.2.0
ARM64 gcc 13.3.0
ARM64 gcc 13.4.0
ARM64 gcc 14.1.0
ARM64 gcc 14.2.0
ARM64 gcc 14.3.0
ARM64 gcc 15.1.0
ARM64 gcc 15.2.0
ARM64 gcc 4.9.4
ARM64 gcc 5.4
ARM64 gcc 5.5.0
ARM64 gcc 6.3
ARM64 gcc 6.4
ARM64 gcc 7.3
ARM64 gcc 7.5
ARM64 gcc 8.2
ARM64 gcc 8.5
ARM64 gcc 9.3
ARM64 gcc 9.4
ARM64 gcc 9.5
ARM64 gcc trunk
ARM64 msvc v19.14 (ex-WINE)
AVR gcc 10.3.0
AVR gcc 11.1.0
AVR gcc 12.1.0
AVR gcc 12.2.0
AVR gcc 12.3.0
AVR gcc 12.4.0
AVR gcc 12.5.0
AVR gcc 13.1.0
AVR gcc 13.2.0
AVR gcc 13.3.0
AVR gcc 13.4.0
AVR gcc 14.1.0
AVR gcc 14.2.0
AVR gcc 14.3.0
AVR gcc 15.1.0
AVR gcc 15.2.0
AVR gcc 4.5.4
AVR gcc 4.6.4
AVR gcc 5.4.0
AVR gcc 9.2.0
AVR gcc 9.3.0
Arduino Mega (1.8.9)
Arduino Uno (1.8.9)
BPF clang (trunk)
BPF clang 13.0.0
BPF clang 14.0.0
BPF clang 15.0.0
BPF clang 16.0.0
BPF clang 17.0.1
BPF clang 18.1.0
BPF clang 19.1.0
BPF clang 20.1.0
BPF clang 21.1.0
EDG (experimental reflection)
EDG 6.5
EDG 6.5 (GNU mode gcc 13)
EDG 6.6
EDG 6.6 (GNU mode gcc 13)
EDG 6.7
EDG 6.7 (GNU mode gcc 14)
FRC 2019
FRC 2020
FRC 2023
HPPA gcc 14.2.0
HPPA gcc 14.3.0
HPPA gcc 15.1.0
HPPA gcc 15.2.0
KVX ACB 4.1.0 (GCC 7.5.0)
KVX ACB 4.1.0-cd1 (GCC 7.5.0)
KVX ACB 4.10.0 (GCC 10.3.1)
KVX ACB 4.11.1 (GCC 10.3.1)
KVX ACB 4.12.0 (GCC 11.3.0)
KVX ACB 4.2.0 (GCC 7.5.0)
KVX ACB 4.3.0 (GCC 7.5.0)
KVX ACB 4.4.0 (GCC 7.5.0)
KVX ACB 4.6.0 (GCC 9.4.1)
KVX ACB 4.8.0 (GCC 9.4.1)
KVX ACB 4.9.0 (GCC 9.4.1)
KVX ACB 5.0.0 (GCC 12.2.1)
KVX ACB 5.2.0 (GCC 13.2.1)
LoongArch64 clang (trunk)
LoongArch64 clang 17.0.1
LoongArch64 clang 18.1.0
LoongArch64 clang 19.1.0
LoongArch64 clang 20.1.0
LoongArch64 clang 21.1.0
M68K gcc 13.1.0
M68K gcc 13.2.0
M68K gcc 13.3.0
M68K gcc 13.4.0
M68K gcc 14.1.0
M68K gcc 14.2.0
M68K gcc 14.3.0
M68K gcc 15.1.0
M68K gcc 15.2.0
M68k clang (trunk)
MRISC32 gcc (trunk)
MSP430 gcc 4.5.3
MSP430 gcc 5.3.0
MSP430 gcc 6.2.1
MinGW clang 14.0.3
MinGW clang 14.0.6
MinGW clang 15.0.7
MinGW clang 16.0.0
MinGW clang 16.0.2
MinGW gcc 11.3.0
MinGW gcc 12.1.0
MinGW gcc 12.2.0
MinGW gcc 13.1.0
MinGW gcc 14.3.0
MinGW gcc 15.2.0
RISC-V (32-bits) gcc (trunk)
RISC-V (32-bits) gcc 10.2.0
RISC-V (32-bits) gcc 10.3.0
RISC-V (32-bits) gcc 11.2.0
RISC-V (32-bits) gcc 11.3.0
RISC-V (32-bits) gcc 11.4.0
RISC-V (32-bits) gcc 12.1.0
RISC-V (32-bits) gcc 12.2.0
RISC-V (32-bits) gcc 12.3.0
RISC-V (32-bits) gcc 12.4.0
RISC-V (32-bits) gcc 12.5.0
RISC-V (32-bits) gcc 13.1.0
RISC-V (32-bits) gcc 13.2.0
RISC-V (32-bits) gcc 13.3.0
RISC-V (32-bits) gcc 13.4.0
RISC-V (32-bits) gcc 14.1.0
RISC-V (32-bits) gcc 14.2.0
RISC-V (32-bits) gcc 14.3.0
RISC-V (32-bits) gcc 15.1.0
RISC-V (32-bits) gcc 15.2.0
RISC-V (32-bits) gcc 8.2.0
RISC-V (32-bits) gcc 8.5.0
RISC-V (32-bits) gcc 9.4.0
RISC-V (64-bits) gcc (trunk)
RISC-V (64-bits) gcc 10.2.0
RISC-V (64-bits) gcc 10.3.0
RISC-V (64-bits) gcc 11.2.0
RISC-V (64-bits) gcc 11.3.0
RISC-V (64-bits) gcc 11.4.0
RISC-V (64-bits) gcc 12.1.0
RISC-V (64-bits) gcc 12.2.0
RISC-V (64-bits) gcc 12.3.0
RISC-V (64-bits) gcc 12.4.0
RISC-V (64-bits) gcc 12.5.0
RISC-V (64-bits) gcc 13.1.0
RISC-V (64-bits) gcc 13.2.0
RISC-V (64-bits) gcc 13.3.0
RISC-V (64-bits) gcc 13.4.0
RISC-V (64-bits) gcc 14.1.0
RISC-V (64-bits) gcc 14.2.0
RISC-V (64-bits) gcc 14.3.0
RISC-V (64-bits) gcc 15.1.0
RISC-V (64-bits) gcc 15.2.0
RISC-V (64-bits) gcc 8.2.0
RISC-V (64-bits) gcc 8.5.0
RISC-V (64-bits) gcc 9.4.0
RISC-V rv32gc clang (trunk)
RISC-V rv32gc clang 10.0.0
RISC-V rv32gc clang 10.0.1
RISC-V rv32gc clang 11.0.0
RISC-V rv32gc clang 11.0.1
RISC-V rv32gc clang 12.0.0
RISC-V rv32gc clang 12.0.1
RISC-V rv32gc clang 13.0.0
RISC-V rv32gc clang 13.0.1
RISC-V rv32gc clang 14.0.0
RISC-V rv32gc clang 15.0.0
RISC-V rv32gc clang 16.0.0
RISC-V rv32gc clang 17.0.1
RISC-V rv32gc clang 18.1.0
RISC-V rv32gc clang 19.1.0
RISC-V rv32gc clang 20.1.0
RISC-V rv32gc clang 21.1.0
RISC-V rv32gc clang 9.0.0
RISC-V rv32gc clang 9.0.1
RISC-V rv64gc clang (trunk)
RISC-V rv64gc clang 10.0.0
RISC-V rv64gc clang 10.0.1
RISC-V rv64gc clang 11.0.0
RISC-V rv64gc clang 11.0.1
RISC-V rv64gc clang 12.0.0
RISC-V rv64gc clang 12.0.1
RISC-V rv64gc clang 13.0.0
RISC-V rv64gc clang 13.0.1
RISC-V rv64gc clang 14.0.0
RISC-V rv64gc clang 15.0.0
RISC-V rv64gc clang 16.0.0
RISC-V rv64gc clang 17.0.1
RISC-V rv64gc clang 18.1.0
RISC-V rv64gc clang 19.1.0
RISC-V rv64gc clang 20.1.0
RISC-V rv64gc clang 21.1.0
RISC-V rv64gc clang 9.0.0
RISC-V rv64gc clang 9.0.1
Raspbian Buster
Raspbian Stretch
SPARC LEON gcc 12.2.0
SPARC LEON gcc 12.3.0
SPARC LEON gcc 12.4.0
SPARC LEON gcc 12.5.0
SPARC LEON gcc 13.1.0
SPARC LEON gcc 13.2.0
SPARC LEON gcc 13.3.0
SPARC LEON gcc 13.4.0
SPARC LEON gcc 14.1.0
SPARC LEON gcc 14.2.0
SPARC LEON gcc 14.3.0
SPARC LEON gcc 15.1.0
SPARC LEON gcc 15.2.0
SPARC gcc 12.2.0
SPARC gcc 12.3.0
SPARC gcc 12.4.0
SPARC gcc 12.5.0
SPARC gcc 13.1.0
SPARC gcc 13.2.0
SPARC gcc 13.3.0
SPARC gcc 13.4.0
SPARC gcc 14.1.0
SPARC gcc 14.2.0
SPARC gcc 14.3.0
SPARC gcc 15.1.0
SPARC gcc 15.2.0
SPARC64 gcc 12.2.0
SPARC64 gcc 12.3.0
SPARC64 gcc 12.4.0
SPARC64 gcc 12.5.0
SPARC64 gcc 13.1.0
SPARC64 gcc 13.2.0
SPARC64 gcc 13.3.0
SPARC64 gcc 13.4.0
SPARC64 gcc 14.1.0
SPARC64 gcc 14.2.0
SPARC64 gcc 14.3.0
SPARC64 gcc 15.1.0
SPARC64 gcc 15.2.0
TI C6x gcc 12.2.0
TI C6x gcc 12.3.0
TI C6x gcc 12.4.0
TI C6x gcc 12.5.0
TI C6x gcc 13.1.0
TI C6x gcc 13.2.0
TI C6x gcc 13.3.0
TI C6x gcc 13.4.0
TI C6x gcc 14.1.0
TI C6x gcc 14.2.0
TI C6x gcc 14.3.0
TI C6x gcc 15.1.0
TI C6x gcc 15.2.0
TI CL430 21.6.1
Tricore gcc 11.3.0 (EEESlab)
VAX gcc NetBSDELF 10.4.0
VAX gcc NetBSDELF 10.5.0 (Nov 15 03:50:22 2023)
VAX gcc NetBSDELF 12.4.0 (Apr 16 05:27 2025)
WebAssembly clang (trunk)
Xtensa ESP32 gcc 11.2.0 (2022r1)
Xtensa ESP32 gcc 12.2.0 (20230208)
Xtensa ESP32 gcc 14.2.0 (20241119)
Xtensa ESP32 gcc 8.2.0 (2019r2)
Xtensa ESP32 gcc 8.2.0 (2020r1)
Xtensa ESP32 gcc 8.2.0 (2020r2)
Xtensa ESP32 gcc 8.4.0 (2020r3)
Xtensa ESP32 gcc 8.4.0 (2021r1)
Xtensa ESP32 gcc 8.4.0 (2021r2)
Xtensa ESP32-S2 gcc 11.2.0 (2022r1)
Xtensa ESP32-S2 gcc 12.2.0 (20230208)
Xtensa ESP32-S2 gcc 14.2.0 (20241119)
Xtensa ESP32-S2 gcc 8.2.0 (2019r2)
Xtensa ESP32-S2 gcc 8.2.0 (2020r1)
Xtensa ESP32-S2 gcc 8.2.0 (2020r2)
Xtensa ESP32-S2 gcc 8.4.0 (2020r3)
Xtensa ESP32-S2 gcc 8.4.0 (2021r1)
Xtensa ESP32-S2 gcc 8.4.0 (2021r2)
Xtensa ESP32-S3 gcc 11.2.0 (2022r1)
Xtensa ESP32-S3 gcc 12.2.0 (20230208)
Xtensa ESP32-S3 gcc 14.2.0 (20241119)
Xtensa ESP32-S3 gcc 8.4.0 (2020r3)
Xtensa ESP32-S3 gcc 8.4.0 (2021r1)
Xtensa ESP32-S3 gcc 8.4.0 (2021r2)
arm64 msvc v19.20 VS16.0
arm64 msvc v19.21 VS16.1
arm64 msvc v19.22 VS16.2
arm64 msvc v19.23 VS16.3
arm64 msvc v19.24 VS16.4
arm64 msvc v19.25 VS16.5
arm64 msvc v19.27 VS16.7
arm64 msvc v19.28 VS16.8
arm64 msvc v19.28 VS16.9
arm64 msvc v19.29 VS16.10
arm64 msvc v19.29 VS16.11
arm64 msvc v19.30 VS17.0
arm64 msvc v19.31 VS17.1
arm64 msvc v19.32 VS17.2
arm64 msvc v19.33 VS17.3
arm64 msvc v19.34 VS17.4
arm64 msvc v19.35 VS17.5
arm64 msvc v19.36 VS17.6
arm64 msvc v19.37 VS17.7
arm64 msvc v19.38 VS17.8
arm64 msvc v19.39 VS17.9
arm64 msvc v19.40 VS17.10
arm64 msvc v19.41 VS17.11
arm64 msvc v19.42 VS17.12
arm64 msvc v19.43 VS17.13
arm64 msvc v19.latest
armv7-a clang (trunk)
armv7-a clang 10.0.0
armv7-a clang 10.0.1
armv7-a clang 11.0.0
armv7-a clang 11.0.1
armv7-a clang 12.0.0
armv7-a clang 12.0.1
armv7-a clang 13.0.0
armv7-a clang 13.0.1
armv7-a clang 14.0.0
armv7-a clang 15.0.0
armv7-a clang 16.0.0
armv7-a clang 17.0.1
armv7-a clang 18.1.0
armv7-a clang 19.1.0
armv7-a clang 20.1.0
armv7-a clang 21.1.0
armv7-a clang 9.0.0
armv7-a clang 9.0.1
armv8-a clang (all architectural features, trunk)
armv8-a clang (trunk)
armv8-a clang 10.0.0
armv8-a clang 10.0.1
armv8-a clang 11.0.0
armv8-a clang 11.0.1
armv8-a clang 12.0.0
armv8-a clang 13.0.0
armv8-a clang 14.0.0
armv8-a clang 15.0.0
armv8-a clang 16.0.0
armv8-a clang 17.0.1
armv8-a clang 18.1.0
armv8-a clang 19.1.0
armv8-a clang 20.1.0
armv8-a clang 21.1.0
armv8-a clang 9.0.0
armv8-a clang 9.0.1
clad trunk (clang 21.1.0)
clad v1.10 (clang 20.1.0)
clad v1.8 (clang 18.1.0)
clad v1.9 (clang 19.1.0)
clad v2.00 (clang 20.1.0)
clad v2.1 (clang 21.1.0)
clang-cl 18.1.0
ellcc 0.1.33
ellcc 0.1.34
ellcc 2017-07-16
ez80-clang 15.0.0
ez80-clang 15.0.7
hexagon-clang 16.0.5
llvm-mos atari2600-3e
llvm-mos atari2600-4k
llvm-mos atari2600-common
llvm-mos atari5200-supercart
llvm-mos atari8-cart-megacart
llvm-mos atari8-cart-std
llvm-mos atari8-cart-xegs
llvm-mos atari8-common
llvm-mos atari8-dos
llvm-mos c128
llvm-mos c64
llvm-mos commodore
llvm-mos cpm65
llvm-mos cx16
llvm-mos dodo
llvm-mos eater
llvm-mos mega65
llvm-mos nes
llvm-mos nes-action53
llvm-mos nes-cnrom
llvm-mos nes-gtrom
llvm-mos nes-mmc1
llvm-mos nes-mmc3
llvm-mos nes-nrom
llvm-mos nes-unrom
llvm-mos nes-unrom-512
llvm-mos osi-c1p
llvm-mos pce
llvm-mos pce-cd
llvm-mos pce-common
llvm-mos pet
llvm-mos rp6502
llvm-mos rpc8e
llvm-mos supervision
llvm-mos vic20
loongarch64 gcc 12.2.0
loongarch64 gcc 12.3.0
loongarch64 gcc 12.4.0
loongarch64 gcc 12.5.0
loongarch64 gcc 13.1.0
loongarch64 gcc 13.2.0
loongarch64 gcc 13.3.0
loongarch64 gcc 13.4.0
loongarch64 gcc 14.1.0
loongarch64 gcc 14.2.0
loongarch64 gcc 14.3.0
loongarch64 gcc 15.1.0
loongarch64 gcc 15.2.0
mips clang 13.0.0
mips clang 14.0.0
mips clang 15.0.0
mips clang 16.0.0
mips clang 17.0.1
mips clang 18.1.0
mips clang 19.1.0
mips clang 20.1.0
mips clang 21.1.0
mips gcc 11.2.0
mips gcc 12.1.0
mips gcc 12.2.0
mips gcc 12.3.0
mips gcc 12.4.0
mips gcc 12.5.0
mips gcc 13.1.0
mips gcc 13.2.0
mips gcc 13.3.0
mips gcc 13.4.0
mips gcc 14.1.0
mips gcc 14.2.0
mips gcc 14.3.0
mips gcc 15.1.0
mips gcc 15.2.0
mips gcc 4.9.4
mips gcc 5.4
mips gcc 5.5.0
mips gcc 9.3.0 (codescape)
mips gcc 9.5.0
mips64 (el) gcc 12.1.0
mips64 (el) gcc 12.2.0
mips64 (el) gcc 12.3.0
mips64 (el) gcc 12.4.0
mips64 (el) gcc 12.5.0
mips64 (el) gcc 13.1.0
mips64 (el) gcc 13.2.0
mips64 (el) gcc 13.3.0
mips64 (el) gcc 13.4.0
mips64 (el) gcc 14.1.0
mips64 (el) gcc 14.2.0
mips64 (el) gcc 14.3.0
mips64 (el) gcc 15.1.0
mips64 (el) gcc 15.2.0
mips64 (el) gcc 4.9.4
mips64 (el) gcc 5.4.0
mips64 (el) gcc 5.5.0
mips64 (el) gcc 9.5.0
mips64 clang 13.0.0
mips64 clang 14.0.0
mips64 clang 15.0.0
mips64 clang 16.0.0
mips64 clang 17.0.1
mips64 clang 18.1.0
mips64 clang 19.1.0
mips64 clang 20.1.0
mips64 clang 21.1.0
mips64 gcc 11.2.0
mips64 gcc 12.1.0
mips64 gcc 12.2.0
mips64 gcc 12.3.0
mips64 gcc 12.4.0
mips64 gcc 12.5.0
mips64 gcc 13.1.0
mips64 gcc 13.2.0
mips64 gcc 13.3.0
mips64 gcc 13.4.0
mips64 gcc 14.1.0
mips64 gcc 14.2.0
mips64 gcc 14.3.0
mips64 gcc 15.1.0
mips64 gcc 15.2.0
mips64 gcc 4.9.4
mips64 gcc 5.4.0
mips64 gcc 5.5.0
mips64 gcc 9.5.0
mips64el clang 13.0.0
mips64el clang 14.0.0
mips64el clang 15.0.0
mips64el clang 16.0.0
mips64el clang 17.0.1
mips64el clang 18.1.0
mips64el clang 19.1.0
mips64el clang 20.1.0
mips64el clang 21.1.0
mipsel clang 13.0.0
mipsel clang 14.0.0
mipsel clang 15.0.0
mipsel clang 16.0.0
mipsel clang 17.0.1
mipsel clang 18.1.0
mipsel clang 19.1.0
mipsel clang 20.1.0
mipsel clang 21.1.0
mipsel gcc 12.1.0
mipsel gcc 12.2.0
mipsel gcc 12.3.0
mipsel gcc 12.4.0
mipsel gcc 12.5.0
mipsel gcc 13.1.0
mipsel gcc 13.2.0
mipsel gcc 13.3.0
mipsel gcc 13.4.0
mipsel gcc 14.1.0
mipsel gcc 14.2.0
mipsel gcc 14.3.0
mipsel gcc 15.1.0
mipsel gcc 15.2.0
mipsel gcc 4.9.4
mipsel gcc 5.4.0
mipsel gcc 5.5.0
mipsel gcc 9.5.0
nanoMIPS gcc 6.3.0 (mtk)
power gcc 11.2.0
power gcc 12.1.0
power gcc 12.2.0
power gcc 12.3.0
power gcc 12.4.0
power gcc 12.5.0
power gcc 13.1.0
power gcc 13.2.0
power gcc 13.3.0
power gcc 13.4.0
power gcc 14.1.0
power gcc 14.2.0
power gcc 14.3.0
power gcc 15.1.0
power gcc 15.2.0
power gcc 4.8.5
power64 AT12.0 (gcc8)
power64 AT13.0 (gcc9)
power64 gcc 11.2.0
power64 gcc 12.1.0
power64 gcc 12.2.0
power64 gcc 12.3.0
power64 gcc 12.4.0
power64 gcc 12.5.0
power64 gcc 13.1.0
power64 gcc 13.2.0
power64 gcc 13.3.0
power64 gcc 13.4.0
power64 gcc 14.1.0
power64 gcc 14.2.0
power64 gcc 14.3.0
power64 gcc 15.1.0
power64 gcc 15.2.0
power64 gcc trunk
power64le AT12.0 (gcc8)
power64le AT13.0 (gcc9)
power64le clang (trunk)
power64le gcc 11.2.0
power64le gcc 12.1.0
power64le gcc 12.2.0
power64le gcc 12.3.0
power64le gcc 12.4.0
power64le gcc 12.5.0
power64le gcc 13.1.0
power64le gcc 13.2.0
power64le gcc 13.3.0
power64le gcc 13.4.0
power64le gcc 14.1.0
power64le gcc 14.2.0
power64le gcc 14.3.0
power64le gcc 15.1.0
power64le gcc 15.2.0
power64le gcc 6.3.0
power64le gcc trunk
powerpc64 clang (trunk)
qnx 8.0.0
s390x gcc 11.2.0
s390x gcc 12.1.0
s390x gcc 12.2.0
s390x gcc 12.3.0
s390x gcc 12.4.0
s390x gcc 12.5.0
s390x gcc 13.1.0
s390x gcc 13.2.0
s390x gcc 13.3.0
s390x gcc 13.4.0
s390x gcc 14.1.0
s390x gcc 14.2.0
s390x gcc 14.3.0
s390x gcc 15.1.0
s390x gcc 15.2.0
sh gcc 12.2.0
sh gcc 12.3.0
sh gcc 12.4.0
sh gcc 12.5.0
sh gcc 13.1.0
sh gcc 13.2.0
sh gcc 13.3.0
sh gcc 13.4.0
sh gcc 14.1.0
sh gcc 14.2.0
sh gcc 14.3.0
sh gcc 15.1.0
sh gcc 15.2.0
sh gcc 4.9.4
sh gcc 9.5.0
vast (trunk)
x64 msvc v19.0 (ex-WINE)
x64 msvc v19.10 (ex-WINE)
x64 msvc v19.14 (ex-WINE)
x64 msvc v19.20 VS16.0
x64 msvc v19.21 VS16.1
x64 msvc v19.22 VS16.2
x64 msvc v19.23 VS16.3
x64 msvc v19.24 VS16.4
x64 msvc v19.25 VS16.5
x64 msvc v19.27 VS16.7
x64 msvc v19.28 VS16.8
x64 msvc v19.28 VS16.9
x64 msvc v19.29 VS16.10
x64 msvc v19.29 VS16.11
x64 msvc v19.30 VS17.0
x64 msvc v19.31 VS17.1
x64 msvc v19.32 VS17.2
x64 msvc v19.33 VS17.3
x64 msvc v19.34 VS17.4
x64 msvc v19.35 VS17.5
x64 msvc v19.36 VS17.6
x64 msvc v19.37 VS17.7
x64 msvc v19.38 VS17.8
x64 msvc v19.39 VS17.9
x64 msvc v19.40 VS17.10
x64 msvc v19.41 VS17.11
x64 msvc v19.42 VS17.12
x64 msvc v19.43 VS17.13
x64 msvc v19.latest
x86 djgpp 4.9.4
x86 djgpp 5.5.0
x86 djgpp 6.4.0
x86 djgpp 7.2.0
x86 msvc v19.0 (ex-WINE)
x86 msvc v19.10 (ex-WINE)
x86 msvc v19.14 (ex-WINE)
x86 msvc v19.20 VS16.0
x86 msvc v19.21 VS16.1
x86 msvc v19.22 VS16.2
x86 msvc v19.23 VS16.3
x86 msvc v19.24 VS16.4
x86 msvc v19.25 VS16.5
x86 msvc v19.27 VS16.7
x86 msvc v19.28 VS16.8
x86 msvc v19.28 VS16.9
x86 msvc v19.29 VS16.10
x86 msvc v19.29 VS16.11
x86 msvc v19.30 VS17.0
x86 msvc v19.31 VS17.1
x86 msvc v19.32 VS17.2
x86 msvc v19.33 VS17.3
x86 msvc v19.34 VS17.4
x86 msvc v19.35 VS17.5
x86 msvc v19.36 VS17.6
x86 msvc v19.37 VS17.7
x86 msvc v19.38 VS17.8
x86 msvc v19.39 VS17.9
x86 msvc v19.40 VS17.10
x86 msvc v19.41 VS17.11
x86 msvc v19.42 VS17.12
x86 msvc v19.43 VS17.13
x86 msvc v19.latest
x86 nvc++ 22.11
x86 nvc++ 22.7
x86 nvc++ 22.9
x86 nvc++ 23.1
x86 nvc++ 23.11
x86 nvc++ 23.3
x86 nvc++ 23.5
x86 nvc++ 23.7
x86 nvc++ 23.9
x86 nvc++ 24.1
x86 nvc++ 24.11
x86 nvc++ 24.3
x86 nvc++ 24.5
x86 nvc++ 24.7
x86 nvc++ 24.9
x86 nvc++ 25.1
x86 nvc++ 25.3
x86 nvc++ 25.5
x86 nvc++ 25.7
x86 nvc++ 25.9
x86-64 Zapcc 190308
x86-64 clang (-fimplicit-constexpr)
x86-64 clang (Chris Bazley N3089)
x86-64 clang (EricWF contracts)
x86-64 clang (amd-staging)
x86-64 clang (assertions trunk)
x86-64 clang (clangir)
x86-64 clang (experimental -Wlifetime)
x86-64 clang (experimental P1061)
x86-64 clang (experimental P1144)
x86-64 clang (experimental P1221)
x86-64 clang (experimental P2998)
x86-64 clang (experimental P3068)
x86-64 clang (experimental P3309)
x86-64 clang (experimental P3367)
x86-64 clang (experimental P3372)
x86-64 clang (experimental P3385)
x86-64 clang (experimental P3776)
x86-64 clang (experimental metaprogramming - P2632)
x86-64 clang (old concepts branch)
x86-64 clang (p1974)
x86-64 clang (pattern matching - P2688)
x86-64 clang (reflection - C++26)
x86-64 clang (reflection - TS)
x86-64 clang (resugar)
x86-64 clang (string interpolation - P3412)
x86-64 clang (thephd.dev)
x86-64 clang (trunk)
x86-64 clang (variadic friends - P2893)
x86-64 clang (widberg)
x86-64 clang 10.0.0
x86-64 clang 10.0.0 (assertions)
x86-64 clang 10.0.1
x86-64 clang 11.0.0
x86-64 clang 11.0.0 (assertions)
x86-64 clang 11.0.1
x86-64 clang 12.0.0
x86-64 clang 12.0.0 (assertions)
x86-64 clang 12.0.1
x86-64 clang 13.0.0
x86-64 clang 13.0.0 (assertions)
x86-64 clang 13.0.1
x86-64 clang 14.0.0
x86-64 clang 14.0.0 (assertions)
x86-64 clang 15.0.0
x86-64 clang 15.0.0 (assertions)
x86-64 clang 16.0.0
x86-64 clang 16.0.0 (assertions)
x86-64 clang 17.0.1
x86-64 clang 17.0.1 (assertions)
x86-64 clang 18.1.0
x86-64 clang 18.1.0 (assertions)
x86-64 clang 19.1.0
x86-64 clang 19.1.0 (assertions)
x86-64 clang 2.6.0 (assertions)
x86-64 clang 2.7.0 (assertions)
x86-64 clang 2.8.0 (assertions)
x86-64 clang 2.9.0 (assertions)
x86-64 clang 20.1.0
x86-64 clang 20.1.0 (assertions)
x86-64 clang 21.1.0
x86-64 clang 21.1.0 (assertions)
x86-64 clang 3.0.0
x86-64 clang 3.0.0 (assertions)
x86-64 clang 3.1
x86-64 clang 3.1 (assertions)
x86-64 clang 3.2
x86-64 clang 3.2 (assertions)
x86-64 clang 3.3
x86-64 clang 3.3 (assertions)
x86-64 clang 3.4 (assertions)
x86-64 clang 3.4.1
x86-64 clang 3.5
x86-64 clang 3.5 (assertions)
x86-64 clang 3.5.1
x86-64 clang 3.5.2
x86-64 clang 3.6
x86-64 clang 3.6 (assertions)
x86-64 clang 3.7
x86-64 clang 3.7 (assertions)
x86-64 clang 3.7.1
x86-64 clang 3.8
x86-64 clang 3.8 (assertions)
x86-64 clang 3.8.1
x86-64 clang 3.9.0
x86-64 clang 3.9.0 (assertions)
x86-64 clang 3.9.1
x86-64 clang 4.0.0
x86-64 clang 4.0.0 (assertions)
x86-64 clang 4.0.1
x86-64 clang 5.0.0
x86-64 clang 5.0.0 (assertions)
x86-64 clang 5.0.1
x86-64 clang 5.0.2
x86-64 clang 6.0.0
x86-64 clang 6.0.0 (assertions)
x86-64 clang 6.0.1
x86-64 clang 7.0.0
x86-64 clang 7.0.0 (assertions)
x86-64 clang 7.0.1
x86-64 clang 7.1.0
x86-64 clang 8.0.0
x86-64 clang 8.0.0 (assertions)
x86-64 clang 8.0.1
x86-64 clang 9.0.0
x86-64 clang 9.0.0 (assertions)
x86-64 clang 9.0.1
x86-64 clang rocm-4.5.2
x86-64 clang rocm-5.0.2
x86-64 clang rocm-5.1.3
x86-64 clang rocm-5.2.3
x86-64 clang rocm-5.3.3
x86-64 clang rocm-5.7.0
x86-64 clang rocm-6.0.2
x86-64 clang rocm-6.1.2
x86-64 clang rocm-6.2.4
x86-64 clang rocm-6.3.3
x86-64 clang rocm-6.4.0
x86-64 clang rocm-7.0.1
x86-64 gcc (P2034 lambdas)
x86-64 gcc (contract labels)
x86-64 gcc (contracts natural syntax)
x86-64 gcc (contracts)
x86-64 gcc (coroutines)
x86-64 gcc (modules)
x86-64 gcc (trunk)
x86-64 gcc 10.1
x86-64 gcc 10.2
x86-64 gcc 10.3
x86-64 gcc 10.3 (assertions)
x86-64 gcc 10.4
x86-64 gcc 10.4 (assertions)
x86-64 gcc 10.5
x86-64 gcc 10.5 (assertions)
x86-64 gcc 11.1
x86-64 gcc 11.1 (assertions)
x86-64 gcc 11.2
x86-64 gcc 11.2 (assertions)
x86-64 gcc 11.3
x86-64 gcc 11.3 (assertions)
x86-64 gcc 11.4
x86-64 gcc 11.4 (assertions)
x86-64 gcc 12.1
x86-64 gcc 12.1 (assertions)
x86-64 gcc 12.2
x86-64 gcc 12.2 (assertions)
x86-64 gcc 12.3
x86-64 gcc 12.3 (assertions)
x86-64 gcc 12.4
x86-64 gcc 12.4 (assertions)
x86-64 gcc 12.5
x86-64 gcc 12.5 (assertions)
x86-64 gcc 13.1
x86-64 gcc 13.1 (assertions)
x86-64 gcc 13.2
x86-64 gcc 13.2 (assertions)
x86-64 gcc 13.3
x86-64 gcc 13.3 (assertions)
x86-64 gcc 13.4
x86-64 gcc 13.4 (assertions)
x86-64 gcc 14.1
x86-64 gcc 14.1 (assertions)
x86-64 gcc 14.2
x86-64 gcc 14.2 (assertions)
x86-64 gcc 14.3
x86-64 gcc 14.3 (assertions)
x86-64 gcc 15.1
x86-64 gcc 15.1 (assertions)
x86-64 gcc 15.2
x86-64 gcc 15.2 (assertions)
x86-64 gcc 3.4.6
x86-64 gcc 4.0.4
x86-64 gcc 4.1.2
x86-64 gcc 4.4.7
x86-64 gcc 4.5.3
x86-64 gcc 4.6.4
x86-64 gcc 4.7.1
x86-64 gcc 4.7.2
x86-64 gcc 4.7.3
x86-64 gcc 4.7.4
x86-64 gcc 4.8.1
x86-64 gcc 4.8.2
x86-64 gcc 4.8.3
x86-64 gcc 4.8.4
x86-64 gcc 4.8.5
x86-64 gcc 4.9.0
x86-64 gcc 4.9.1
x86-64 gcc 4.9.2
x86-64 gcc 4.9.3
x86-64 gcc 4.9.4
x86-64 gcc 5.1
x86-64 gcc 5.2
x86-64 gcc 5.3
x86-64 gcc 5.4
x86-64 gcc 5.5
x86-64 gcc 6.1
x86-64 gcc 6.2
x86-64 gcc 6.3
x86-64 gcc 6.4
x86-64 gcc 6.5
x86-64 gcc 7.1
x86-64 gcc 7.2
x86-64 gcc 7.3
x86-64 gcc 7.4
x86-64 gcc 7.5
x86-64 gcc 8.1
x86-64 gcc 8.2
x86-64 gcc 8.3
x86-64 gcc 8.4
x86-64 gcc 8.5
x86-64 gcc 9.1
x86-64 gcc 9.2
x86-64 gcc 9.3
x86-64 gcc 9.4
x86-64 gcc 9.5
x86-64 icc 13.0.1
x86-64 icc 16.0.3
x86-64 icc 17.0.0
x86-64 icc 18.0.0
x86-64 icc 19.0.0
x86-64 icc 19.0.1
x86-64 icc 2021.1.2
x86-64 icc 2021.10.0
x86-64 icc 2021.2.0
x86-64 icc 2021.3.0
x86-64 icc 2021.4.0
x86-64 icc 2021.5.0
x86-64 icc 2021.6.0
x86-64 icc 2021.7.0
x86-64 icc 2021.7.1
x86-64 icc 2021.8.0
x86-64 icc 2021.9.0
x86-64 icx 2021.1.2
x86-64 icx 2021.2.0
x86-64 icx 2021.3.0
x86-64 icx 2021.4.0
x86-64 icx 2022.0.0
x86-64 icx 2022.1.0
x86-64 icx 2022.2.0
x86-64 icx 2022.2.1
x86-64 icx 2023.0.0
x86-64 icx 2023.1.0
x86-64 icx 2023.2.1
x86-64 icx 2024.0.0
x86-64 icx 2024.1.0
x86-64 icx 2024.2.0
x86-64 icx 2024.2.1
x86-64 icx 2025.0.0
x86-64 icx 2025.0.1
x86-64 icx 2025.0.3
x86-64 icx 2025.0.4
x86-64 icx 2025.1.0
x86-64 icx 2025.1.1
x86-64 icx 2025.2.0
x86-64 icx 2025.2.1
x86-64 icx 2025.2.1
z180-clang 15.0.0
z180-clang 15.0.7
z80-clang 15.0.0
z80-clang 15.0.7
zig c++ 0.10.0
zig c++ 0.11.0
zig c++ 0.12.0
zig c++ 0.12.1
zig c++ 0.13.0
zig c++ 0.14.0
zig c++ 0.14.1
zig c++ 0.15.1
zig c++ 0.6.0
zig c++ 0.7.0
zig c++ 0.7.1
zig c++ 0.8.0
zig c++ 0.9.0
zig c++ trunk
Options
Source code
#include <algorithm> #include <cassert> #include <expected> #include <functional> #include <numeric> #include <ranges> #include <sstream> #include <string_view> enum class decodexx_error : signed char { illegal_character, missing_character, illegal_padding, non_canonical, }; template <typename I, typename O> struct decodexx_error_result { std::ranges::in_out_result<I, O> in_out_result; decodexx_error error; }; namespace detail { namespace basexx { constexpr auto padding = '='; enum class identifier : unsigned char { base16, base32, base32hex, base64, }; template <identifier id> struct alphabet {}; template <identifier id> inline constexpr auto alphabet_v = alphabet<id>::value; template <> struct alphabet<identifier::base16> { static constexpr auto value = std::string_view{"0123456789ABCDEF"}; }; template <> struct alphabet<identifier::base32> { static constexpr auto value = std::string_view{"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"}; }; template <> struct alphabet<identifier::base32hex> { static constexpr auto value = std::string_view{"0123456789ABCDEFGHIJKLMNOPQRSTUV"}; }; template <> struct alphabet<identifier::base64> { static constexpr auto value = std::string_view{ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"}; }; template <identifier id> inline constexpr std::size_t alphabet_size{alphabet_v<id>.size()}; template <identifier id> struct shift_register {}; template <identifier id> using shift_register_t = shift_register<id>::type; template <> struct shift_register<identifier::base16> { using type = unsigned char; }; template <identifier id> requires(id == identifier::base32 or id == identifier::base32hex) struct shift_register<id> { using type = unsigned long long; }; template <> struct shift_register<identifier::base64> { using type = unsigned long; }; template <identifier id> struct alphabet_properties { static constexpr int width{std::countr_zero(alphabet_size<id>)}; static constexpr int octet_width{8}; static constexpr int block_width{std::lcm(width, octet_width)}; static constexpr int block_num_octets{block_width / octet_width}; static constexpr int block_num_chars{block_width / width}; static_assert(static_cast<int>(sizeof(shift_register_t<id>)) >= block_num_octets); }; template <identifier id> struct encode : std::ranges::range_adaptor_closure<encode<id>> { template <std::ranges::viewable_range R> requires( std::ranges::forward_range<R> and std::convertible_to<std::ranges::range_reference_t<R>, std::byte>) constexpr auto operator()(R &&r) const { using props = basexx::alphabet_properties<id>; return std::views::cartesian_product( std::forward<R>(r) | std::views::chunk(props::block_num_octets), std::views::iota(0, static_cast<int>(props::block_num_chars)) | std::views::reverse) | std::views::transform([](auto indexed_chunk) -> char { auto [chunk, i] = indexed_chunk; auto j = props::block_num_octets - 1; unsigned char c{}; for (const auto octet : chunk) { c |= std::rotr( static_cast<basexx::shift_register_t<id>>(octet), (props::width * i) - (props::octet_width * j--)); } return i < (props::block_num_chars - (props::octet_width * (props::block_num_octets - j - 1) + props::width - 1) / props::width) ? basexx::padding : alphabet_v<id>.at(c & (alphabet_size<id> - 1)); }); } }; } // namespace basexx namespace decodexx { template <basexx::identifier id> class max_size { static constexpr auto input_size_to_max_size(size_t input_size) -> std::expected<size_t, decodexx_error> { using props = basexx::alphabet_properties<id>; if ((input_size % props::block_num_chars) != 0) { return std::unexpected{decodexx_error::missing_character}; } return input_size / props::block_num_chars * props::block_num_octets; } public: template <typename R> requires(std::ranges::sized_range<R> or std::ranges::forward_range<R>) // NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward) constexpr auto operator()(R &&r) const -> std::expected<size_t, decodexx_error> { if constexpr (std::ranges::sized_range<R>) { return input_size_to_max_size(std::ranges::size(r)); } else { return input_size_to_max_size(std::ranges::distance(r)); } } }; } // namespace decodexx namespace basexx { template <identifier id> struct try_decode { template <std::input_iterator I, std::sentinel_for<I> S, std::weakly_incrementable O, typename Proj = std::identity> requires(std::convertible_to<std::indirect_result_t<Proj, I>, char> and std::indirectly_writable<O, std::byte>) constexpr auto operator()(I first, S last, O result, Proj proj = {}) const -> std::expected<std::ranges::in_out_result<I, O>, decodexx_error_result<I, O>> { using namespace std::views; using lookup_t = std::array<unsigned char, std::numeric_limits<unsigned char>::max() + 1>; // The following flags would not work for any alphabet size, but they // work up to 64, which is enough for us. static constexpr auto is_valid = 0x40; static constexpr auto is_padding = 0x80; static constexpr auto lookup = [] -> lookup_t { lookup_t a{}; unsigned char i{}; for (const unsigned char c : basexx::alphabet_v<id>) { a.at(c) = is_valid | i++; } a.at(static_cast<unsigned char>('=')) = is_padding | is_valid; return a; }(); auto make_error_result = [first = std::move(first), result = std::move(result)](decodexx_error error) mutable { return std::unexpected{decodexx_error_result<I, O>{ .in_out_result = {std::move(first), std::move(result)}, .error = error, }}; }; while (first != last) { using props = alphabet_properties<id>; basexx::shift_register_t<id> shift_register{}; auto num_padding = 0; auto i = 0; for (i = 0; i < props::block_num_chars and first != last; ++i, ++first) { const auto bitset = lookup.at( static_cast<unsigned char>(std::invoke(proj, *first))); if (not(bitset & is_valid)) { return make_error_result(decodexx_error::illegal_character); } const auto bitset_is_padding = static_cast<int>(bitset >> 7); if ((i < 2 and bitset_is_padding) or (num_padding and not bitset_is_padding)) { return make_error_result(decodexx_error::illegal_padding); } num_padding += bitset_is_padding; shift_register = (shift_register << props::width) | (bitset & (alphabet_size<id> - 1)); } if (i != props::block_num_chars) { return make_error_result(decodexx_error::missing_character); } const int num_missing_octets{ (num_padding * props::width + props::octet_width - 1) / props::octet_width}; if (shift_register & ((decltype(shift_register){1} << (num_missing_octets * props::octet_width)) - 1)) { return make_error_result(decodexx_error::non_canonical); } for (i = 0; i < props::block_num_octets - num_missing_octets; ++i) { *result++ = static_cast<std::byte>( shift_register >> ((props::block_num_octets - i - 1) * props::octet_width)); } } return std::ranges::in_out_result<I, O>{std::move(first), std::move(result)}; } template <std::ranges::input_range R, std::weakly_incrementable O, typename Proj = std::identity> requires(std::convertible_to< std::indirect_result_t<Proj, std::ranges::iterator_t<R>>, char> and std::indirectly_writable<O, std::byte>) // NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward) constexpr auto operator()(R &&r, O result, Proj proj = {}) const -> std::expected< std::ranges::in_out_result<std::ranges::borrowed_iterator_t<R>, O>, decodexx_error_result<std::ranges::borrowed_iterator_t<R>, O>> { return (*this)(std::ranges::begin(r), std::ranges::end(r), std::move(result), std::move(proj)) .transform([](auto &&expected) { return std::ranges::in_out_result< std::ranges::borrowed_iterator_t<R>, O>{ .in{std::move(expected.in)}, .out{std::move(expected.out)}, }; }) .transform_error([](auto &&error) { return decodexx_error_result< std::ranges::borrowed_iterator_t<R>, O>{ .in_out_result{ .in{std::move(error.in_out_result.in)}, .out{std::move(error.in_out_result.out)}, }, .error{error.error}, }; }); } }; template <identifier id, template <typename> typename C> struct try_decode_to { template <std::ranges::input_range R, typename Proj = std::identity> requires std::convertible_to< std::indirect_result_t<Proj, std::ranges::iterator_t<R>>, char> constexpr auto operator()(R &&r, Proj proj = {}) const -> std::expected<C<std::byte>, decodexx_error>; }; template <identifier id> struct try_decode_to<id, std::vector> { template <std::ranges::input_range R, typename Proj = std::identity> requires std::convertible_to< std::indirect_result_t<Proj, std::ranges::iterator_t<R>>, char> constexpr auto operator()(R &&r, Proj proj = {}) const -> std::expected<std::vector<std::byte>, decodexx_error> { std::vector<std::byte> v{}; return try_decode<id>{}(std::forward<R>(r), std::back_insert_iterator{v}, std::move(proj)) .transform( [v = std::move(v)](auto &&) mutable { return std::move(v); }) .transform_error([](const auto &error) { return error.error; }); } template <std::ranges::input_range R, typename Proj = std::identity> requires(std::convertible_to< std::indirect_result_t<Proj, std::ranges::iterator_t<R>>, char> and (std::ranges::sized_range<R> or std::ranges::forward_range<R>)) constexpr auto operator()(R &&r, Proj proj = {}) const -> std::expected<std::vector<std::byte>, decodexx_error> { const auto size = decodexx::max_size<id>{}(std::forward<R>(r)); if (not size.has_value()) { return std::unexpected{size.error()}; } std::vector<std::byte> v(*size); return try_decode<id>{}(std::forward<R>(r), v.begin(), std::move(proj)) .transform([v = std::move(v)](const auto &result) mutable { v.resize(result.out - v.begin()); return std::move(v); }) .transform_error([](const auto &error) { return error.error; }); } }; template <identifier id, template <typename> typename C> requires std::default_initializable<C<std::byte>> struct decode_to { template <std::ranges::input_range R, typename Proj = std::identity> requires std::convertible_to< std::indirect_result_t<Proj, std::ranges::iterator_t<R>>, char> constexpr auto operator()(R &&r, Proj proj = {}) const -> C<std::byte> { return try_decode_to<id, C>{}(std::forward<R>(r), std::move(proj)) .value_or(C<std::byte>{}); } }; } // namespace basexx } // namespace detail inline constexpr detail::basexx::encode<detail::basexx::identifier::base16> encode16{}; inline constexpr detail::basexx::encode<detail::basexx::identifier::base32> encode32{}; inline constexpr detail::basexx::encode<detail::basexx::identifier::base32hex> encode32hex{}; inline constexpr detail::basexx::encode<detail::basexx::identifier::base64> encode64{}; inline constexpr detail::decodexx::max_size<detail::basexx::identifier::base16> decode16_max_size{}; inline constexpr detail::decodexx::max_size<detail::basexx::identifier::base32> decode32_max_size{}; inline constexpr detail::decodexx::max_size< detail::basexx::identifier::base32hex> decode64_max_size{}; inline constexpr detail::basexx::try_decode<detail::basexx::identifier::base16> try_decode16{}; inline constexpr detail::basexx::try_decode<detail::basexx::identifier::base32> try_decode32{}; inline constexpr detail::basexx::try_decode< detail::basexx::identifier::base32hex> try_decode32hex{}; inline constexpr detail::basexx::try_decode<detail::basexx::identifier::base64> try_decode64{}; inline constexpr detail::basexx::try_decode_to< detail::basexx::identifier::base16, std::vector> try_decode16_to_vector{}; inline constexpr detail::basexx::try_decode_to< detail::basexx::identifier::base32, std::vector> try_decode32_to_vector{}; inline constexpr detail::basexx::try_decode_to< detail::basexx::identifier::base32hex, std::vector> try_decode32hex_to_vector{}; inline constexpr detail::basexx::try_decode_to< detail::basexx::identifier::base64, std::vector> try_decode64_to_vector{}; inline constexpr detail::basexx::decode_to<detail::basexx::identifier::base16, std::vector> decode16_to_vector{}; inline constexpr detail::basexx::decode_to<detail::basexx::identifier::base32, std::vector> decode32_to_vector{}; inline constexpr detail::basexx::decode_to< detail::basexx::identifier::base32hex, std::vector> decode32hex_to_vector{}; inline constexpr detail::basexx::decode_to<detail::basexx::identifier::base64, std::vector> decode64_to_vector{}; namespace { using namespace std::string_view_literals; using std::ranges::equal; constexpr auto to_bytes = std::views::transform( [](auto num) -> std::byte { return static_cast<std::byte>(num); }); void test_encode16() { static constexpr auto encode = to_bytes | encode16; // RFC 4648 test vectors ("foobar" in ASCII) static_assert(equal(""sv | encode, ""sv)); static_assert(equal("\x{66}"sv | encode, "66"sv)); static_assert(equal("\x{66}\x{6F}"sv | encode, "666F"sv)); static_assert(equal("\x{66}\x{6F}\x{6F}"sv | encode, "666F6F"sv)); static_assert(equal("\x{66}\x{6F}\x{6F}\x{62}"sv | encode, "666F6F62"sv)); static_assert( equal("\x{66}\x{6F}\x{6F}\x{62}\x{61}"sv | encode, "666F6F6261"sv)); static_assert(equal("\x{66}\x{6F}\x{6F}\x{62}\x{61}\x{72}"sv | encode, "666F6F626172"sv)); } void test_decode16() { // RFC 4648 test vectors ("foobar" in ASCII) static constexpr auto decode = decode16_to_vector; static_assert(equal(decode(""sv), ""sv | to_bytes)); static_assert(equal(decode("66"sv), "\x{66}"sv | to_bytes)); static_assert(equal(decode("666F"sv), "\x{66}\x{6F}"sv | to_bytes)); static_assert(equal(decode("666F6F"sv), "\x{66}\x{6F}\x{6F}"sv | to_bytes)); static_assert( equal(decode("666F6F62"sv), "\x{66}\x{6F}\x{6F}\x{62}"sv | to_bytes)); static_assert(equal(decode("666F6F6261"sv), "\x{66}\x{6F}\x{6F}\x{62}\x{61}"sv | to_bytes)); static_assert(equal(decode("666F6F626172"sv), "\x{66}\x{6F}\x{6F}\x{62}\x{61}\x{72}"sv | to_bytes)); } void test_decode16_error_handling() { static constexpr auto decode = try_decode16_to_vector; static_assert(not decode("Z123"sv).has_value() and decode("Z123"sv).error() == decodexx_error::illegal_character); static_assert(not decode("\x{00}123"sv).has_value() and decode("\x{00}123"sv).error() == decodexx_error::illegal_character); static_assert(not decode("\x{FF}123"sv).has_value() and decode("\x{FF}123"sv).error() == decodexx_error::illegal_character); static_assert(not decode("A1234"sv).has_value() and decode("A1234"sv).error() == decodexx_error::missing_character); // Note: flagging illegal padding, as opposed to illegal character, could // seem strange in the case of Base16, since padding is never needed in that // case. But RFC 4648 specifies "=" as the general padding character, so // flagging its use in Base16 as illegal padding is not a violation of the // standard. On the other hand, the non-canonical error cannot occur in // Base16, because it can only occur if actual padding is used. static_assert(not decode("1=34"sv).has_value() and decode("1=34"sv).error() == decodexx_error::illegal_padding); static_assert(not decode("12=4"sv).has_value() and decode("12=4"sv).error() == decodexx_error::illegal_padding); } void test_encode32() { static constexpr auto encode = to_bytes | encode32; // RFC 4648 test vectors ("foobar" in ASCII) static_assert(equal(""sv | encode, ""sv)); static_assert(equal("\x{66}"sv | encode, "MY======"sv)); static_assert(equal("\x{66}\x{6F}"sv | encode, "MZXQ===="sv)); static_assert(equal("\x{66}\x{6F}\x{6F}"sv | encode, "MZXW6==="sv)); static_assert(equal("\x{66}\x{6F}\x{6F}\x{62}"sv | encode, "MZXW6YQ="sv)); static_assert( equal("\x{66}\x{6F}\x{6F}\x{62}\x{61}"sv | encode, "MZXW6YTB"sv)); static_assert(equal("\x{66}\x{6F}\x{6F}\x{62}\x{61}\x{72}"sv | encode, "MZXW6YTBOI======"sv)); } void test_decode32() { // RFC 4648 test vectors ("foobar" in ASCII) static constexpr auto decode = decode32_to_vector; static_assert(equal(decode(""sv), ""sv | to_bytes)); static_assert(equal(decode("MY======"sv), "\x{66}"sv | to_bytes)); static_assert(equal(decode("MZXQ===="sv), "\x{66}\x{6F}"sv | to_bytes)); static_assert( equal(decode("MZXW6==="sv), "\x{66}\x{6F}\x{6F}"sv | to_bytes)); static_assert( equal(decode("MZXW6YQ="sv), "\x{66}\x{6F}\x{6F}\x{62}"sv | to_bytes)); static_assert(equal(decode("MZXW6YTB"sv), "\x{66}\x{6F}\x{6F}\x{62}\x{61}"sv | to_bytes)); static_assert(equal(decode("MZXW6YTBOI======"sv), "\x{66}\x{6F}\x{6F}\x{62}\x{61}\x{72}"sv | to_bytes)); } void test_decode32_error_handling() { static constexpr auto decode = try_decode32_to_vector; static_assert(not decode("MYVA\x{00}LUE"sv).has_value() and decode("MYVA\x{00}LUE"sv).error() == decodexx_error::illegal_character); static_assert(not decode("MYVA\x{FF}LUE"sv).has_value() and decode("MYVA\x{FF}LUE"sv).error() == decodexx_error::illegal_character); static_assert(not decode("MYV\x{FF}LUE"sv).has_value() and decode("MYV\x{FF}LUE"sv).error() == decodexx_error::missing_character); static_assert(not decode("M=YV\x{FF}LUE"sv).has_value() and decode("M=YV\x{FF}LUE"sv).error() == decodexx_error::illegal_padding); static_assert(not decode("MY==A==="sv).has_value() and decode("MY==A==="sv).error() == decodexx_error::illegal_padding); static_assert(not decode("MZ======"sv).has_value() and decode("MZ======"sv).error() == decodexx_error::non_canonical); static_assert(not decode("M7======"sv).has_value() and decode("M7======"sv).error() == decodexx_error::non_canonical); static_assert(not decode("MY======A"sv).has_value() and decode("MY======A"sv).error() == decodexx_error::missing_character); } void test_encode32hex() { static constexpr auto encode = to_bytes | encode32hex; // RFC 4648 test vectors ("foobar" in ASCII) static_assert(equal(""sv | encode, ""sv)); static_assert(equal("\x{66}"sv | encode, "CO======"sv)); static_assert(equal("\x{66}\x{6F}"sv | encode, "CPNG===="sv)); static_assert(equal("\x{66}\x{6F}\x{6F}"sv | encode, "CPNMU==="sv)); static_assert(equal("\x{66}\x{6F}\x{6F}\x{62}"sv | encode, "CPNMUOG="sv)); static_assert( equal("\x{66}\x{6F}\x{6F}\x{62}\x{61}"sv | encode, "CPNMUOJ1"sv)); static_assert(equal("\x{66}\x{6F}\x{6F}\x{62}\x{61}\x{72}"sv | encode, "CPNMUOJ1E8======"sv)); } void test_decode32hex() { // RFC 4648 test vectors ("foobar" in ASCII) static constexpr auto decode = decode32hex_to_vector; static_assert(equal(decode(""sv), ""sv | to_bytes)); static_assert(equal(decode("CO======"sv), "\x{66}"sv | to_bytes)); static_assert(equal(decode("CPNG===="sv), "\x{66}\x{6F}"sv | to_bytes)); static_assert( equal(decode("CPNMU==="sv), "\x{66}\x{6F}\x{6F}"sv | to_bytes)); static_assert( equal(decode("CPNMUOG="sv), "\x{66}\x{6F}\x{6F}\x{62}"sv | to_bytes)); static_assert(equal(decode("CPNMUOJ1"sv), "\x{66}\x{6F}\x{6F}\x{62}\x{61}"sv | to_bytes)); static_assert(equal(decode("CPNMUOJ1E8======"sv), "\x{66}\x{6F}\x{6F}\x{62}\x{61}\x{72}"sv | to_bytes)); } void test_encode64() { static constexpr auto encode = to_bytes | encode64; // "Man" in ASCII static_assert(equal("\x{4D}\x{61}\x{6E}"sv | encode, "TWFu"sv)); static_assert(equal("\x{4D}\x{61}"sv | encode, "TWE="sv)); static_assert(equal("\x{4D}"sv | encode, "TQ=="sv)); // "light work." in ASCII static_assert(equal( "\x{6C}\x{69}\x{67}\x{68}\x{74}\x{20}\x{77}\x{6F}\x{72}\x{6B}\x{2E}"sv | encode, "bGlnaHQgd29yay4="sv)); static_assert( equal("\x{6C}\x{69}\x{67}\x{68}\x{74}\x{20}\x{77}\x{6F}\x{72}\x{6B}"sv | encode, "bGlnaHQgd29yaw=="sv)); static_assert(equal( "\x{6C}\x{69}\x{67}\x{68}\x{74}\x{20}\x{77}\x{6F}\x{72}"sv | encode, "bGlnaHQgd29y"sv)); static_assert( equal("\x{6C}\x{69}\x{67}\x{68}\x{74}\x{20}\x{77}\x{6F}"sv | encode, "bGlnaHQgd28="sv)); static_assert(equal("\x{6C}\x{69}\x{67}\x{68}\x{74}\x{20}\x{77}"sv | encode, "bGlnaHQgdw=="sv)); // "Many hands make light work." in ASCII constexpr auto many_etc = "\x{4D}\x{61}\x{6E}\x{79}\x{20}\x{68}\x{61}\x{6E}\x{64}\x{73}\x{20}\x{6D}\x{61}\x{6B}\x{65}\x{20}"sv "\x{6C}\x{69}\x{67}\x{68}\x{74}\x{20}\x{77}\x{6F}\x{72}\x{6B}\x{2E}"sv; static_assert( equal(many_etc | encode, "TWFueSBoYW5kcyBtYWtlIGxpZ2h0IHdvcmsu"sv)); // RFC 4648 test vectors ("foobar" in ASCII) static_assert(equal(""sv | encode, ""sv)); static_assert(equal("\x{66}"sv | encode, "Zg=="sv)); static_assert(equal("\x{66}\x{6F}"sv | encode, "Zm8="sv)); static_assert(equal("\x{66}\x{6F}\x{6F}"sv | encode, "Zm9v"sv)); static_assert(equal("\x{66}\x{6F}\x{6F}\x{62}"sv | encode, "Zm9vYg=="sv)); static_assert( equal("\x{66}\x{6F}\x{6F}\x{62}\x{61}"sv | encode, "Zm9vYmE="sv)); static_assert( equal("\x{66}\x{6F}\x{6F}\x{62}\x{61}\x{72}"sv | encode, "Zm9vYmFy"sv)); } void test_encode64_preservation_of_range_properties() { static constexpr auto encode = to_bytes | encode64; // "Many hands make light work." in ASCII constexpr auto many_etc = "\x{4D}\x{61}\x{6E}\x{79}\x{20}\x{68}\x{61}\x{6E}\x{64}\x{73}\x{20}\x{6D}\x{61}\x{6B}\x{65}\x{20}"sv "\x{6C}\x{69}\x{67}\x{68}\x{74}\x{20}\x{77}\x{6F}\x{72}\x{6B}\x{2E}"sv; static_assert( std::ranges::random_access_range<decltype(many_etc | encode)>); static_assert(std::ranges::sized_range<decltype(many_etc | encode)>); static_assert(std::ranges::size(many_etc | encode) == 36); } void test_decode64() { static constexpr auto decode = decode64_to_vector; // "Man" in ASCII static_assert(equal(decode("TWFu"sv), "\x{4D}\x{61}\x{6E}"sv | to_bytes)); static_assert(equal(decode("TWE="sv), "\x{4D}\x{61}"sv | to_bytes)); static_assert(equal(decode("TQ=="sv), "\x{4D}"sv | to_bytes)); // "light work." in ASCII static_assert(equal( decode("bGlnaHQgd29yay4="sv), "\x{6C}\x{69}\x{67}\x{68}\x{74}\x{20}\x{77}\x{6F}\x{72}\x{6B}\x{2E}"sv | to_bytes)); static_assert( equal(decode("bGlnaHQgd29yaw=="sv), "\x{6C}\x{69}\x{67}\x{68}\x{74}\x{20}\x{77}\x{6F}\x{72}\x{6B}"sv | to_bytes)); static_assert(equal( decode("bGlnaHQgd29y"sv), "\x{6C}\x{69}\x{67}\x{68}\x{74}\x{20}\x{77}\x{6F}\x{72}"sv | to_bytes)); static_assert( equal(decode("bGlnaHQgd28="sv), "\x{6C}\x{69}\x{67}\x{68}\x{74}\x{20}\x{77}\x{6F}"sv | to_bytes)); static_assert( equal(decode("bGlnaHQgdw=="sv), "\x{6C}\x{69}\x{67}\x{68}\x{74}\x{20}\x{77}"sv | to_bytes)); // "Many hands make light work." in ASCII constexpr auto many_etc = "\x{4D}\x{61}\x{6E}\x{79}\x{20}\x{68}\x{61}\x{6E}\x{64}\x{73}\x{20}\x{6D}\x{61}\x{6B}\x{65}\x{20}"sv "\x{6C}\x{69}\x{67}\x{68}\x{74}\x{20}\x{77}\x{6F}\x{72}\x{6B}\x{2E}"sv; static_assert(equal(decode("TWFueSBoYW5kcyBtYWtlIGxpZ2h0IHdvcmsu"sv), many_etc | to_bytes)); // RFC 4648 test vectors ("foobar" in ASCII) static_assert(equal(decode(""sv), ""sv | to_bytes)); static_assert(equal(decode("Zg=="sv), "\x{66}"sv | to_bytes)); static_assert(equal(decode("Zm8="sv), "\x{66}\x{6F}"sv | to_bytes)); static_assert(equal(decode("Zm9v"sv), "\x{66}\x{6F}\x{6F}"sv | to_bytes)); static_assert( equal(decode("Zm9vYg=="sv), "\x{66}\x{6F}\x{6F}\x{62}"sv | to_bytes)); static_assert(equal(decode("Zm9vYmE="sv), "\x{66}\x{6F}\x{6F}\x{62}\x{61}"sv | to_bytes)); static_assert(equal(decode("Zm9vYmFy"sv), "\x{66}\x{6F}\x{6F}\x{62}\x{61}\x{72}"sv | to_bytes)); } void test_decode64_error_handling() { static constexpr auto decode = try_decode64_to_vector; static_assert(not decode("TWFuT"sv).has_value() and decode("TWFuT"sv).error() == decodexx_error::missing_character); static_assert(not decode("bGlnaHQgd2=="sv).has_value() and decode("bGlnaHQgd2=="sv).error() == decodexx_error::non_canonical); static_assert(not decode("bGlnaH\x{00}gd28="sv).has_value() and decode("bGlnaH\x{00}gd28="sv).error() == decodexx_error::illegal_character); static_assert(not decode("bGlnaH\x{FF}gd28="sv).has_value() and decode("bGlnaH\x{FF}gd28="sv).error() == decodexx_error::illegal_character); static_assert(not decode("bGln=HQgd28="sv).has_value() and decode("bGln=HQgd28="sv).error() == decodexx_error::illegal_padding); } void test_decode64_input_range() { std::istringstream many_etc{"TWFueSBoYW5kcyBtYWtlIGxpZ2h0IHdvcmsu"}; auto many_etc_view = std::views::istream<char>(many_etc); assert(equal( decode64_to_vector(many_etc_view), "\x{4D}\x{61}\x{6E}\x{79}\x{20}\x{68}\x{61}\x{6E}\x{64}\x{73}\x{20}\x{6D}\x{61}\x{6B}\x{65}\x{20}"sv "\x{6C}\x{69}\x{67}\x{68}\x{74}\x{20}\x{77}\x{6F}\x{72}\x{6B}\x{2E}"sv | to_bytes)); } void test_decode64_input_range_error_handling() { std::istringstream missing_character{"TWFuT"}; auto missing_character_view = std::views::istream<char>(missing_character); const auto missing_char_result = try_decode64_to_vector(missing_character_view); assert(not missing_char_result.has_value()); assert(missing_char_result.error() == decodexx_error::missing_character); } void test_encode64_decode64() { constexpr auto man = "\x{4D}\x{61}\x{6E}"sv; static_assert( equal(decode64_to_vector(man | to_bytes | encode64), man | to_bytes)); } void test_decode64_encode64() { constexpr auto encoded = "TWFu"sv; static_assert(equal(decode64_to_vector(encoded) | encode64, encoded)); } } // namespace auto main() -> int { test_encode16(); test_decode16(); test_decode16_error_handling(); test_encode32(); test_decode32(); test_decode32_error_handling(); test_encode32hex(); test_decode32hex(); // Note: no explicit check for Base32 Hex error handling, since only the // alphabet symbols differ from regular Base32. test_encode64(); test_decode64(); test_decode64_error_handling(); // "Special functionality" only tested for Base64 to avoid even more // verbosity. Base16 and Base32 should give the same results. Also worth // noting: input range encoding is at the time of writing not provided, // because the best solution to run our algorithm without multi-pass would // be to use views::cache_last between views::transform and // views::cartesian_product, but that facility at the time of writing is // only proposed for standardization // (https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p3138r0.html). // Also, using views::cache_last will not be sufficient: in order for our // implementation not to be ill-formed with input ranges, we will also need // to implement a variant of std::transform that does not require // equality-preservation (see // https://stackoverflow.com/questions/79069912/stdviewschunk-stdviewstransform-inputs-ranges-and-ill-formedness). test_encode64_preservation_of_range_properties(); test_decode64_input_range(); test_decode64_input_range_error_handling(); test_encode64_decode64(); test_decode64_encode64(); return 0; }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Compiler Explorer Shop
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
CE on Bluesky
Statistics
Changelog
Version tree