1 module dcord.util..string; 2 3 import std..string; 4 import std.array; 5 import std.ascii : isLower, isUpper; 6 7 /// Convert a camelCase string to a snake_case one. This is a pure function 8 pure string camelCaseToUnderscores(string input) { 9 auto stringBuilder = appender!string; 10 stringBuilder.reserve(input.length * 2); 11 bool previousWasLower = false; 12 13 foreach (c; input) { 14 if(previousWasLower && c.isUpper()) stringBuilder.put('_'); 15 16 if(c.isLower()) previousWasLower = true; 17 else previousWasLower = false; 18 19 stringBuilder.put(c); 20 } 21 22 return stringBuilder.data.toLower(); 23 }