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