drop sentinal on return to snakeToCamel

This commit is contained in:
Emil Lerch 2023-08-27 19:54:51 -07:00
parent 17ff6d3e82
commit 47fbda5e06
Signed by: lobo
GPG Key ID: A7B62D657EF764F8

View File

@ -1,12 +1,11 @@
const std = @import("std"); const std = @import("std");
const expectEqualStrings = std.testing.expectEqualStrings; const expectEqualStrings = std.testing.expectEqualStrings;
pub fn snakeToCamel(allocator: std.mem.Allocator, name: []const u8) ![:0]u8 { 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: usize = 0; var target_inx: usize = 0;
var previous_ascii: u8 = 0; var previous_ascii: u8 = 0;
// A single word will take the entire length plus our sentinel var rc = try allocator.alloc(u8, name.len);
var 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 = @as(u8, @truncate(cp)); const ascii_char = @as(u8, @truncate(cp));
@ -22,9 +21,8 @@ pub fn snakeToCamel(allocator: std.mem.Allocator, name: []const u8) ![:0]u8 {
previous_ascii = ascii_char; previous_ascii = ascii_char;
} }
// Do we care if the allocator refuses resize? // Do we care if the allocator refuses resize?
_ = allocator.resize(rc, target_inx + 1); _ = allocator.resize(rc, target_inx);
rc[target_inx] = 0; // add zero sentinel return rc[0..target_inx];
return rc[0..target_inx :0];
} }
pub fn snakeToPascal(allocator: std.mem.Allocator, name: []const u8) ![]u8 { pub fn snakeToPascal(allocator: std.mem.Allocator, name: []const u8) ![]u8 {
const rc = try snakeToCamel(allocator, name); const rc = try snakeToCamel(allocator, name);