aws-sdk-for-zig/src/case.zig

50 lines
1.8 KiB
Zig
Raw Normal View History

const std = @import("std");
const expectEqualStrings = std.testing.expectEqualStrings;
2023-08-27 15:40:40 +00:00
pub fn snakeToCamel(allocator: std.mem.Allocator, name: []const u8) ![:0]u8 {
var utf8_name = (std.unicode.Utf8View.init(name) catch unreachable).iterator();
2022-01-20 20:31:45 +00:00
var target_inx: usize = 0;
var previous_ascii: u8 = 0;
// A single word will take the entire length plus our sentinel
2023-08-27 15:40:40 +00:00
var rc = try allocator.alloc(u8, name.len + 1);
while (utf8_name.nextCodepoint()) |cp| {
if (cp > 0xff) return error.UnicodeNotSupported;
2023-08-04 17:07:58 +00:00
const ascii_char = @as(u8, @truncate(cp));
if (ascii_char != '_') {
if (previous_ascii == '_' and ascii_char >= 'a' and ascii_char <= 'z') {
const uppercase_char = ascii_char - ('a' - 'A');
rc[target_inx] = uppercase_char;
} else {
rc[target_inx] = ascii_char;
}
target_inx = target_inx + 1;
}
previous_ascii = ascii_char;
}
2023-08-27 15:40:40 +00:00
// Do we care if the allocator refuses resize?
_ = allocator.resize(rc, target_inx + 1);
rc[target_inx] = 0; // add zero sentinel
2023-08-27 15:40:40 +00:00
return rc[0..target_inx :0];
}
2021-12-23 16:51:48 +00:00
pub fn snakeToPascal(allocator: std.mem.Allocator, name: []const u8) ![]u8 {
const rc = try snakeToCamel(allocator, name);
if (rc[0] >= 'a' and rc[0] <= 'z') {
const uppercase_char = rc[0] - ('a' - 'A');
rc[0] = uppercase_char;
}
return rc;
}
test "converts from snake to camelCase" {
const allocator = std.testing.allocator;
const camel = try snakeToCamel(allocator, "access_key_id");
defer allocator.free(camel);
try expectEqualStrings("accessKeyId", camel);
}
test "single word" {
const allocator = std.testing.allocator;
const camel = try snakeToCamel(allocator, "word");
defer allocator.free(camel);
try expectEqualStrings("word", camel);
}