const std = @import("std"); pub const ciutf8 = @import("./ciutf8.zig"); pub const Uuid = @import("./Uuid.zig"); pub const DateTime = @import("./DateTime.zig"); pub const PathIter = @import("./PathIter.zig"); pub const case = struct { // returns the number of capital letters in a string. // only works with ascii characters fn countCaps(str: []const u8) usize { var count: usize = 0; for (str) |ch| { if (std.ascii.isUpper(ch)) { count += 1; } } return count; } // converts a string from PascalCase to snake_case at comptime. // only works with ascii characters pub fn pascalToSnake(comptime str: []const u8) Return: { break :Return if (str.len == 0) *const [0:0]u8 else *const [str.len + countCaps(str) - 1:0]u8; } { comptime { if (str.len == 0) return ""; var buf = std.mem.zeroes([str.len + countCaps(str) - 1:0]u8); var i = 0; for (str) |ch| { if (std.ascii.isUpper(ch)) { if (i != 0) { buf[i] = '_'; i += 1; } buf[i] = std.ascii.toLower(ch); } else { buf[i] = ch; } i += 1; } return &buf; } } }; test "pascalToSnake" { try std.testing.expectEqual("", case.pascalToSnake("")); try std.testing.expectEqual("abc", case.pascalToSnake("Abc")); try std.testing.expectEqual("a_bc", case.pascalToSnake("ABc")); try std.testing.expectEqual("a_b_c", case.pascalToSnake("ABC")); try std.testing.expectEqual("ab_c", case.pascalToSnake("AbC")); } test { _ = ciutf8; _ = Uuid; _ = PathIter; _ = DateTime; }