Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
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
Nim
Objective-C
Objective-C++
OCaml
OpenCL C
Pascal
Pony
Python
Racket
Ruby
Rust
Snowball
Scala
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
WASM
Zig
Javascript
GIMPLE
Ygen
zig 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
zig 0.10.0
zig 0.11.0
zig 0.12.0
zig 0.12.1
zig 0.13.0
zig 0.2.0
zig 0.3.0
zig 0.4.0
zig 0.5.0
zig 0.6.0
zig 0.7.0
zig 0.7.1
zig 0.8.0
zig 0.9.0
zig trunk
Options
Source code
const std = @import("std"); pub fn Implementation() type { const Impl = @OpaqueType(); return struct { const Self = @This(); impl: ?*Impl, pub fn none() Self { return Self{ .impl = null }; } pub fn init(ptr: var) Self { const T = @typeOf(ptr); if (@alignOf(T) == 0) @compileError("0-Bit implementations can't be casted (and casting is unnecessary anyway, use .none)"); return Self{ .impl = @ptrCast(*Impl, ptr) }; } pub fn cast(self: *const Self, comptime T: type) *T { if (@alignOf(T) == 0) @compileError("0-Bit implementations can't be casted (and casting is unnecessary anyway)"); const aligned = @alignCast(@alignOf(T), self.impl); return @ptrCast(*T, aligned); } }; } pub const Comparison = enum { Less, Equal, Greater, }; pub const Comparable = struct { pub const Impl = Implementation(); pub fn compare(self: *Self, other: *Self) Comparison { return self._compare(self, other); } pub fn equal(self: *Self, other: *Self) bool { return self.compare(other) == Comparison.Equal; } pub fn less(self: *Self, other: *Self) bool { return self.compare(other) == Comparison.Less; } pub fn greater(self: *Self, other: *Self) bool { return self.compare(other) == Comparison.Greater; } const Self = @This(); impl: Impl, _compare: fn (self: *Self, other: *Self) Comparison, }; pub const String = struct { const Self = @This(); const Value = []const u8; value: Value, pub fn comparable(self: *Self) Comparable { return Comparable{ .impl = Comparable.Impl.init(self), ._compare = compare, }; } fn compare(self: *const Comparable, other: *const Comparable) Comparison { const a = self.impl.cast(Self).value; const b = other.impl.cast(Self).value; const len = if (a.len < b.len) a.len else b.len; var i: usize = 0; while (i < len and a[i] == b[i]) : (i += 1) {} if (i == len) { if (a.len < b.len) { return Comparison.Less; } else if (a.len > b.len) { return Comparison.Greater; } } else { if (a[i] < b[i]) { return Comparison.Less; } else if (a[i] > b[i]) { return Comparison.Greater; } } return Comparison.Equal; } }; pub fn main() void { var s1 = (String{ .value = "hello" }).comparable(); var s2 = (String{ .value = "hello" }).comparable(); var s3 = (String{ .value = "z" }).comparable(); std.debug.assert(s1.equal(&s2)); std.debug.assert(s1.less(&s3)); std.debug.assert(s3.greater(&s2)); }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
About the author
Statistics
Changelog
Version tree