Camel case and snake case: Difference between revisions

Added XPL0 example.
(Added XPL0 example.)
Line 291:
c://my-docs/happy_Flag-Day/12.doc -> c://myDocs/happyFlagDay/12.doc
spaces -> spaces
</pre>
 
=={{header|XPL0}}==
<lang XPL0>string 0; \use zero-terminated strings
char Out(100); \output string (made global for safety)
 
func Trim(Str); \Trim leading and trailing spaces from string
char Str;
int I;
[while Str(0) = $20 do Str:= Str+1; \skip leading spaces
I:= 0; \skip to end of string (+1)
while Str(I) # 0 do I:= I+1;
while I>0 & Str(I-1)=$20 do I:= I-1; \skip back to first non-space
Str(I):= 0; \chop off any trailing spaces
return Str;
];
 
func ToSnake(In); \Convert string to snake_case
char In;
int I, J, C, UL;
[I:= 0; J:= 0; UL:= true; \suppress leading & redundant underlines
repeat C:= In(I); I:= I+1; \get character from input string
if C>=^A & C<=^Z then \convert uppercase to "_" + lowercase
[if not UL then [Out(J):= ^_; J:= J+1];
Out(J):= C+$20; J:= J+1;
UL:= false;
]
else if C=$20 or C=^- then \convert to underlines
[if not UL then [Out(J):= ^_; J:= J+1; UL:= true]
]
else [Out(J):= C; J:= J+1; UL:= C=^_];
until C = 0;
return Out;
];
 
func ToCamel(In); \Convert string to camelCase
char In;
int I, J, C;
[I:= 0; J:= 0;
repeat C:= In(I); I:= I+1;
if C=^_ or C=^- or C=$20 then
[C:= In(I); I:= I+1;
if C>=^a & C<=^z then C:= C-$20;
Out(J):= C; J:= J+1;
]
else [Out(J):= C; J:= J+1];
until C = 0;
return Out;
];
 
int Strings, I;
[Strings:= [
"snakeCase", "snake_case", "variable_10_case", "variable10Case", "\u025brgo rE tHis",
"hurry-up-joe!", "c://my-docs/happy_Flag-Day/12.doc", " spaces "];
Text(0, "To snake case:^M^J");
for I:= 0 to 7 do
[Text(0, Strings(I));
Text(0, " -> ");
Text(0, ToSnake(Trim(Strings(I))));
CrLf(0);
];
Text(0, "To camel case:^M^J");
for I:= 0 to 7 do
[Text(0, Strings(I));
Text(0, " -> ");
Text(0, ToCamel(Trim(Strings(I))));
CrLf(0);
];
]</lang>
 
{{out}}
<pre>
To snake case:
snakeCase -> snake_case
snake_case -> snake_case
variable_10_case -> variable_10_case
variable10Case -> variable10_case
\u025brgo rE tHis -> \u025brgo_r_e_t_his
hurry-up-joe! -> hurry_up_joe!
c://my-docs/happy_Flag-Day/12.doc -> c://my_docs/happy_flag_day/12.doc
spaces -> spaces
To camel case:
snakeCase -> snakeCase
snake_case -> snakeCase
variable_10_case -> variable10Case
variable10Case -> variable10Case
\u025brgo rE tHis -> \u025brgoRETHis
hurry-up-joe! -> hurryUpJoe!
c://my-docs/happy_Flag-Day/12.doc -> c://myDocs/happyFlagDay/12.doc
spaces -> spaces
</pre>
290

edits