support for single word handling in camelCase algo

This commit is contained in:
Emil Lerch 2021-07-24 00:18:38 -07:00
parent 079677bb2e
commit a97e26477d
Signed by: lobo
GPG Key ID: A7B62D657EF764F8

View File

@ -5,7 +5,8 @@ pub fn snakeToCamel(allocator: *std.mem.Allocator, name: []const u8) ![]u8 {
var utf8_name = (std.unicode.Utf8View.init(name) catch unreachable).iterator(); var utf8_name = (std.unicode.Utf8View.init(name) catch unreachable).iterator();
var target_inx: u64 = 0; var target_inx: u64 = 0;
var previous_ascii: u8 = 0; var previous_ascii: u8 = 0;
const rc = try allocator.alloc(u8, name.len); // This is slightly overkill, will need <= number of input chars // A single word will take the entire length plus our sentinel
const rc = try allocator.alloc(u8, name.len + 1);
while (utf8_name.nextCodepoint()) |cp| { while (utf8_name.nextCodepoint()) |cp| {
if (cp > 0xff) return error.UnicodeNotSupported; if (cp > 0xff) return error.UnicodeNotSupported;
const ascii_char = @truncate(u8, cp); const ascii_char = @truncate(u8, cp);
@ -38,3 +39,9 @@ test "converts from snake to camelCase" {
defer allocator.free(camel); defer allocator.free(camel);
try expectEqualStrings("accessKeyId", 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);
}