1 module henhouse.converter; 2 3 import uni = std.uni; 4 5 6 /** 7 * Converts a single word to its "chicken" representation. 8 * 9 * Params: 10 * original string to convert 11 * 12 * Returns: 13 * converted string 14 */ 15 string wordToChicken(string original) 16 { 17 string chicken; 18 19 for (int i = 0; i < original.length; i++) { 20 if (uni.isLower(original[i])) { 21 chicken ~= "chicken"; 22 23 while ((i + 1 < original.length) && uni.isLower(original[i + 1])) { 24 i++; 25 } 26 27 continue; 28 } 29 30 if (uni.isUpper(original[i])) { 31 if ((i + 1 == original.length) || uni.isLower(original[i + 1])) { 32 chicken ~= "Chicken"; 33 34 while ((i + 1 < original.length) && uni.isLower(original[i + 1])) { 35 i++; 36 } 37 38 continue; 39 } 40 41 if ((i + 1 == original.length) || uni.isUpper(original[i + 1])) { 42 chicken ~= "CHICKEN"; 43 44 while ((i + 1 < original.length) && uni.isUpper(original[i + 1])) { 45 i++; 46 } 47 } 48 } 49 } 50 51 return chicken; 52 } 53 54 unittest 55 { 56 assert("chicken" == wordToChicken("lowercase")); 57 assert("Chicken" == wordToChicken("Capitalized")); 58 assert("CHICKEN" == wordToChicken("UPPERCASE")); 59 60 assert("chickenChicken" == wordToChicken("mixedCapitalized")); 61 assert("ChickenChicken" == wordToChicken("MixedCapitalized")); 62 assert("chickenCHICKEN" == wordToChicken("mixedCAPITALIZED")); 63 }