interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / drafts / rosettacode / camel-case-and-snake-case.wiki

128 lines · 2647 bytes Raw Blame History
1=={{header|Omegese}}==
2{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}}
3Omega has no imports, so the small output helpers (<code>emit</code> and friends) are part of the program.
4<syntaxhighlight lang="text">
5om2
6text nl "\n"
7text tsn " -> snake: "
8text tcm " -> camel: "
9text c0 "snakeCase"
10text c1 "snake_case"
11text c2 "variable_10_case"
12text c3 "variable10Case"
13text c4 "hurry-up-joe!"
14text c5 "c://my-docs/happy_Flag-Day/12.doc"
15text c6 " spaces "
16note emit writes every byte of a to stdout; a short write is retried, an error exits 1
17fn emit a view uchar * to void
18o = ulong 0
19while lt o len a bound 4096
20k = write 1 a o sub len a o
21if le k 0
22exit 1
23end
24o = add o ulong k
25end
26end
27note emitc writes one byte
28fn emitc c uchar to void
29b = new uchar 1 0
30put b 0 c
31emit b
32end
33note leading and trailing spaces are kept as they are; inside the text, space and '-' count as
34note separators like '_'
35fn issep c uchar to bool
36ret or or eq c 95 eq c 45 eq c 32
37end
38fn lead a view uchar * to ulong
39for i ulong len a
40if ne get a i 32
41ret i
42end
43end
44ret len a
45end
46fn trail a view uchar * to ulong
47for i ulong len a
48if ne get a sub sub len a 1 i 32
49ret sub len a i
50end
51end
52ret 0
53end
54note snake: a separator becomes '_', an upper-case letter becomes '_' and its lower case (no '_' first
55note when it is at the start or follows a separator)
56fn snake a view uchar * to void
57l = lead a
58t = trail a
59emit part a 0 l
60for i ulong sub t l
61k = add l i
62c = get a k
63if issep c
64emitc 95
65else
66if and ge c 65 le c 90
67if gt i 0
68if not issep get a sub k 1
69emitc 95
70end
71end
72emitc add c 32
73else
74emitc c
75end
76end
77end
78emit part a t sub len a t
79end
80note camel: separators are dropped and the letter after one is upper-cased
81fn camel a view uchar * to void
82l = lead a
83t = trail a
84emit part a 0 l
85up = false
86for i ulong sub t l
87c = get a add l i
88if issep c
89up = true
90else
91if and up and ge c 97 le c 122
92c = sub c 32
93end
94emitc c
95up = false
96end
97end
98emit part a t sub len a t
99end
100fn show a view uchar * to void
101emit a
102emit tsn
103snake a
104emit tcm
105camel a
106emit nl
107end
108fn main to int
109show c0
110show c1
111show c2
112show c3
113show c4
114show c5
115show c6
116ret 0
117end
118</syntaxhighlight>
119{{out}}
120<pre>
121snakeCase -> snake: snake_case -> camel: snakeCase
122snake_case -> snake: snake_case -> camel: snakeCase
123variable_10_case -> snake: variable_10_case -> camel: variable10Case
124variable10Case -> snake: variable10_case -> camel: variable10Case
125hurry-up-joe! -> snake: hurry_up_joe! -> camel: hurryUpJoe!
126c://my-docs/happy_Flag-Day/12.doc -> snake: c://my_docs/happy_flag_day/12.doc -> camel: c://myDocs/happyFlagDay/12.doc
127 spaces -> snake: spaces -> camel: spaces
128</pre>