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
Clojure
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
Helion
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
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.14.0
zig 0.14.1
zig 0.15.1
zig 0.15.2
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"); fn lineWriter( writer: anytype, comptime line_length: usize, comptime eol: []const u8, ) LineWriter(line_length, eol, @TypeOf(writer)) { return .{ .free_writer = writer }; } fn LineWriter( comptime line_length: usize, // I can't think of a reason for eol being a runtime argument. // You should always know your eol at comptime. // This leads to better optimizations. comptime eol: []const u8, comptime WriterType: type, ) type { return struct { const Remaining = std.math.IntFittingRange(0, line_length); const EolStage = std.math.IntFittingRange(0, eol.len - 1); /// lineWriter takes a writer and an end of line delimiter and ensures that each line is /// at most `g_line_length` length including the end of line delimiter. /// Used in writing vcard specification because it requires each line to be max 1024 in length /// including the \r\n free_writer: WriterType, /// How many bytes can still be written to the line, discounting the eol remaining: Remaining = line_length, /// How many bytes of the eol were written eol_stage: EolStage = 0, pub const Error = WriterType.Error; pub const Writer = std.io.Writer(*Self, Error, write); const Self = @This(); pub fn writer(self: *Self) Writer { return .{ .context = self }; } pub fn write(self: *Self, bytes: []const u8) Error!usize { if (bytes[0] == eol[self.eol_stage]) { // It seems like the caller is writing the eol manually if (self.eol_stage == eol.len - 1) { // Full eol has been written self.eol_stage = 0; self.remaining = line_length; try self.free_writer.writeByte(bytes[0]); return 1; } else { self.eol_stage += 1; } } else { // Not a eol if (self.remaining <= eol.len) { const missing_part = eol.len - self.remaining; if (eol[missing_part] == bytes[0]) { self.eol_stage = @intCast(missing_part + 1); } else { try self.free_writer.writeAll(eol[missing_part..]); try self.free_writer.writeAll(eol[0..missing_part]); self.remaining = line_length; self.eol_stage = 0; } } else { self.eol_stage = 0; } } self.remaining -= 1; try self.free_writer.writeByte(bytes[0]); return 1; } }; } const test_namespace = struct { const line_length = 5; const eol = "\r\n"; fn helper(src: []const u8, comptime expected: []const u8) !void { var actual: [expected.len]u8 = undefined; var stream = std.io.fixedBufferStream(&actual); var lw = lineWriter(stream.writer(), line_length, eol); try lw.writer().writeAll(src); try std.testing.expectEqualSlices(u8, expected, &actual); } fn @"normal use"() !void { try helper("a", "a"); try helper("abc", "abc"); try helper("abcd", "abc\r\nd"); try helper("abcdef", "abc\r\ndef"); try helper("abcdefg", "abc\r\ndef\r\ng"); } fn @"manual eol"() !void{ try helper("\r\n", "\r\n"); try helper("a\r\n", "a\r\n"); try helper("ab\r\n", "ab\r\n"); try helper("ab\r\nc", "ab\r\nc"); try helper("abc\r\n", "abc\r\n"); try helper("abc\r\nd", "abc\r\nd"); try helper("abc\r\ndef", "abc\r\ndef"); try helper("abc\r\ndef\r\n", "abc\r\ndef\r\n"); try helper("abc\r\ndef\r\ng", "abc\r\ndef\r\ng"); } fn @"fake eol"() !void { try helper("\r", "\r"); try helper("\ra", "\ra"); try helper("ab\r", "ab\r"); try helper("ab\rc", "ab\r\r\nc"); try helper("abc\rd", "abc\r\n\rd"); try helper("ab\r\r\ncd", "ab\r\r\ncd"); } }; pub fn main() !void{ try test_namespace.@"normal use"(); try test_namespace.@"manual eol"(); try test_namespace.@"fake eol"(); }
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