interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / camel-case-and-snake-case / camel-case-and-snake-case.om

115 lines · 2067 bytes Raw Blame History
1note Rosetta Code task "Camel case and snake case": https://rosettacode.org/wiki/Camel_case_and_snake_case
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text tsn " -> snake: "
6text tcm " -> camel: "
7text c0 "snakeCase"
8text c1 "snake_case"
9text c2 "variable_10_case"
10text c3 "variable10Case"
11text c4 "hurry-up-joe!"
12text c5 "c://my-docs/happy_Flag-Day/12.doc"
13text c6 " spaces "
14note emit writes every byte of a to stdout; a short write is retried, an error exits 1
15fn emit a view uchar * to void
16o = ulong 0
17while lt o len a bound 4096
18k = write 1 a o sub len a o
19if le k 0
20exit 1
21end
22o = add o ulong k
23end
24end
25note emitc writes one byte
26fn emitc c uchar to void
27b = new uchar 1 0
28put b 0 c
29emit b
30end
31note leading and trailing spaces are kept as they are; inside the text, space and '-' count as
32note separators like '_'
33fn issep c uchar to bool
34ret or or eq c 95 eq c 45 eq c 32
35end
36fn lead a view uchar * to ulong
37for i ulong len a
38if ne get a i 32
39ret i
40end
41end
42ret len a
43end
44fn trail a view uchar * to ulong
45for i ulong len a
46if ne get a sub sub len a 1 i 32
47ret sub len a i
48end
49end
50ret 0
51end
52note snake: a separator becomes '_', an upper-case letter becomes '_' and its lower case (no '_' first
53note when it is at the start or follows a separator)
54fn snake a view uchar * to void
55l = lead a
56t = trail a
57emit part a 0 l
58for i ulong sub t l
59k = add l i
60c = get a k
61if issep c
62emitc 95
63else
64if and ge c 65 le c 90
65if gt i 0
66if not issep get a sub k 1
67emitc 95
68end
69end
70emitc add c 32
71else
72emitc c
73end
74end
75end
76emit part a t sub len a t
77end
78note camel: separators are dropped and the letter after one is upper-cased
79fn camel a view uchar * to void
80l = lead a
81t = trail a
82emit part a 0 l
83up = false
84for i ulong sub t l
85c = get a add l i
86if issep c
87up = true
88else
89if and up and ge c 97 le c 122
90c = sub c 32
91end
92emitc c
93up = false
94end
95end
96emit part a t sub len a t
97end
98fn show a view uchar * to void
99emit a
100emit tsn
101snake a
102emit tcm
103camel a
104emit nl
105end
106fn main to int
107show c0
108show c1
109show c2
110show c3
111show c4
112show c5
113show c6
114ret 0
115end