1 module painlessjson..string;
2 
3 import std..string;
4 import std.conv;
5 import std.array;
6 import std.ascii : isLower, isUpper;
7 
8 version (unittest)
9 {
10     import dunit.toolkit;
11 
12 }
13 
14 unittest {
15     enum hello_world = camelCaseToUnderscore("helloWorld");
16     assertEqual(hello_world,"hello_world");
17     enum my_json = camelCaseToUnderscore("myJSON");
18     assertEqual(my_json,"my_json");
19 }
20 
21 string camelCaseToUnderscore(string input){
22     auto stringBuilder = appender!string;
23     stringBuilder.reserve(input.length*2);
24     bool previousWasLower = false;
25     foreach(c;input){
26         if(previousWasLower && c.isUpper())
27         {
28             stringBuilder.put('_');
29         }
30 
31         if(c.isLower())
32         {
33             previousWasLower = true;
34         } else{
35             previousWasLower = false;
36         }
37         stringBuilder.put(c);
38     }
39     return stringBuilder.data.toLower();
40 }