allow for spaces in the name to be "handled" (ignored)
This commit is contained in:
		
							parent
							
								
									ae7ecb1179
								
							
						
					
					
						commit
						22e39dd6e1
					
				
					 1 changed files with 14 additions and 7 deletions
				
			
		| 
						 | 
					@ -15,6 +15,7 @@ pub fn fromPascalCase(allocator: *std.mem.Allocator, name: []const u8) ![]u8 {
 | 
				
			||||||
        if (codepoint > 0xff) return error{UnicodeNotSupported}.UnicodeNotSupported;
 | 
					        if (codepoint > 0xff) return error{UnicodeNotSupported}.UnicodeNotSupported;
 | 
				
			||||||
        if (next_codepoint > 0xff) return error{UnicodeNotSupported}.UnicodeNotSupported;
 | 
					        if (next_codepoint > 0xff) return error{UnicodeNotSupported}.UnicodeNotSupported;
 | 
				
			||||||
        const ascii_char = @truncate(u8, codepoint);
 | 
					        const ascii_char = @truncate(u8, codepoint);
 | 
				
			||||||
 | 
					        if (next_codepoint == ' ') continue; // ignore all spaces in name
 | 
				
			||||||
        if (ascii_char >= 'A' and ascii_char < 'Z') {
 | 
					        if (ascii_char >= 'A' and ascii_char < 'Z') {
 | 
				
			||||||
            const lowercase_char = ascii_char + ('a' - 'A');
 | 
					            const lowercase_char = ascii_char + ('a' - 'A');
 | 
				
			||||||
            if (previous_codepoint == null) {
 | 
					            if (previous_codepoint == null) {
 | 
				
			||||||
| 
						 | 
					@ -32,11 +33,11 @@ pub fn fromPascalCase(allocator: *std.mem.Allocator, name: []const u8) ![]u8 {
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        } else {
 | 
					        } else {
 | 
				
			||||||
            if (ascii_char == ' ') {
 | 
					            // if (ascii_char == ' ') {
 | 
				
			||||||
                rc[target_inx] = '_';
 | 
					            //     rc[target_inx] = '_';
 | 
				
			||||||
            } else {
 | 
					            // } else {
 | 
				
			||||||
                rc[target_inx] = ascii_char;
 | 
					            rc[target_inx] = ascii_char;
 | 
				
			||||||
            }
 | 
					            // }
 | 
				
			||||||
            target_inx = target_inx + 1;
 | 
					            target_inx = target_inx + 1;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        previous_codepoint = codepoint;
 | 
					        previous_codepoint = codepoint;
 | 
				
			||||||
| 
						 | 
					@ -58,11 +59,17 @@ test "converts from PascalCase to snake_case" {
 | 
				
			||||||
    const allocator = std.testing.allocator;
 | 
					    const allocator = std.testing.allocator;
 | 
				
			||||||
    const snake_case = try fromPascalCase(allocator, "MyPascalCaseThing");
 | 
					    const snake_case = try fromPascalCase(allocator, "MyPascalCaseThing");
 | 
				
			||||||
    defer allocator.free(snake_case);
 | 
					    defer allocator.free(snake_case);
 | 
				
			||||||
    expectEqualStrings("my_pascal_case_thing", snake_case);
 | 
					    try expectEqualStrings("my_pascal_case_thing", snake_case);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
test "handles from PascalCase acronyms to snake_case" {
 | 
					test "handles from PascalCase acronyms to snake_case" {
 | 
				
			||||||
    const allocator = std.testing.allocator;
 | 
					    const allocator = std.testing.allocator;
 | 
				
			||||||
    const snake_case = try fromPascalCase(allocator, "SAMLMySAMLAcronymThing");
 | 
					    const snake_case = try fromPascalCase(allocator, "SAMLMySAMLAcronymThing");
 | 
				
			||||||
    defer allocator.free(snake_case);
 | 
					    defer allocator.free(snake_case);
 | 
				
			||||||
    expectEqualStrings("saml_my_saml_acronym_thing", snake_case);
 | 
					    try expectEqualStrings("saml_my_saml_acronym_thing", snake_case);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					test "spaces in the name" {
 | 
				
			||||||
 | 
					    const allocator = std.testing.allocator;
 | 
				
			||||||
 | 
					    const snake_case = try fromPascalCase(allocator, "API Gateway");
 | 
				
			||||||
 | 
					    defer allocator.free(snake_case);
 | 
				
			||||||
 | 
					    try expectEqualStrings("api_gateway", snake_case);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue