Camel case and snake case: Difference between revisions

Added Easylang
m (Adjusting text formatting.)
(Added Easylang)
 
(6 intermediate revisions by 5 users not shown)
Line 680:
 
 
 
=={{header|EasyLang}}==
{{trans|Nim}}
<syntaxhighlight>
func$ strip s$ .
a = 1
while substr s$ a 1 = " "
a += 1
.
b = len s$
while substr s$ b 1 = " "
b -= 1
.
return substr s$ a (b - a + 1)
.
func$ toupper c$ .
c = strcode c$
if c >= 97 and c <= 122
c$ = strchar (c - 32)
.
return c$
.
func$ tolower c$ .
c = strcode c$
if c >= 65 and c <= 90
c$ = strchar (c + 32)
.
return c$
.
func isupper c$ .
c = strcode c$
if c >= 65 and c <= 90
return 1
.
.
delim$ = "_- "
func$ snakecase s$ .
s$ = strip s$
for c$ in strchars s$
if isupper c$ = 1 and prev$ <> ""
if strpos delim$ prev$ = 0
r$ &= "_"
.
r$ &= tolower c$
else
r$ &= c$
.
prev$ = c$
.
return r$
.
func$ camelcase s$ .
s$ = strip s$
prev$ = "x"
for c$ in strchars s$
if strpos delim$ prev$ <> 0
r$ &= toupper c$
elif strpos delim$ c$ = 0
r$ &= c$
.
prev$ = c$
.
return r$
.
test$[] = [ "snakeCase" "snake_case" "variable_10_case" "variable10Case" "ɛrgo rE tHis" "hurry-up-joe!" "c://my-docs/happy_Flag-Day/12.doc" " spaces " ]
print "=== To snake_case ==="
for s$ in test$[]
print s$ & " -> " & snakecase s$
.
print "\n=== To camelCase ==="
for s$ in test$[]
print s$ & " -> " & camelcase s$
.
</syntaxhighlight>
{{out}}
<pre>
=== To snake_case ===
snakeCase -> snake_case
snake_case -> snake_case
variable_10_case -> variable_10_case
variable10Case -> variable10_case
ɛrgo rE tHis -> ɛrgo 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 camelCase ===
snakeCase -> snakeCase
snake_case -> snakeCase
variable_10_case -> variable10Case
variable10Case -> variable10Case
ɛrgo rE tHis -> ɛrgoRETHis
hurry-up-joe! -> hurryUpJoe!
c://my-docs/happy_Flag-Day/12.doc -> c://myDocs/happyFlagDay/12.doc
spaces -> spaces
</pre>
 
=={{header|Factor}}==
Line 738 ⟶ 834:
" internal space " >camel " internalSpace "
</pre>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="vb">Function isDigit(ch As String) As Boolean
Return ch >= "0" And ch <= "9"
End Function
 
Function to_snake_case(s As String) As String
Dim As Integer l = Len(s)
Dim As String snake = Trim(s), tmp = snake
For i As Integer = 1 To Len(snake)
If isDigit(Mid(snake, i, 1)) Then
Continue For
Elseif Instr(Mid(snake, i, 1), " ") Then
Mid(snake, i) = "_"
Continue For
Elseif Instr(Mid(snake, i, 1), Any "\:/-_!.") Then
Continue For
Elseif Mid(snake, i, 1) = Ucase(Mid(snake, i, 1)) Then
tmp = Lcase(Mid(snake, i,1))
Mid(snake, i) = "_"
snake = Left(snake, i) & tmp & Mid(snake, i+1)
End If
Next i
Return snake
End Function
 
Function toCamelCase(s As String) As String
Dim As Integer l = Len(s)
Dim As String camel = Trim(s), tmp = camel
For i As Integer = 1 To Len(camel)
If Instr(Mid(camel, i, 1), Any ":/!.") Then
Continue For
Elseif Instr(Mid(camel, i, 1), Any " _-") Then
camel = Left(camel, i-1) & Ucase(Mid(camel, i+1,1)) & Mid(camel, i+2)
End If
Next i
Return camel
End Function
 
Dim Shared tests(1 To ...) As String*33 => {_
"snakeCase", "snake_case", "variable_10_case", "variable10Case", _
"\u025brgo rE tHis", "ergo rE tHis", "hurry-up-joe!", _
"c://my-docs/happy_Flag-Day/12.doc", " spaces "}
 
Sub test0(title As String, fn As String)
Print title
For i As Integer = 1 To Ubound(tests)
Dim As String texto = tests(i) & " ===> " & to_snake_case(tests(i))
Locate i+1, 41 - Len(texto) / 2 : Print texto
Next i
End Sub
 
Sub test1(title As String, fn As String)
Print title
For i As Integer = 1 To Ubound(tests)
Dim As String texto = tests(i) & " ===> " & toCamelCase(tests(i))
Locate i+12, 41 - Len(texto) / 2 : Print texto
Next i
End Sub
 
test0 "to_snake_case:", "to_snake_case"
Print
test1 "toCamelCase:", "toCamelCase"
 
Sleep</syntaxhighlight>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
 
include "NSLog.incl"
local fn snake_toCamel( s as CFStringRef ) as CFStringRef
long r,f
s = fn StringByTrimmingCharactersInSet( s, fn CFCharacterSetGetPredefined(_kCFCharacterSetWhitespace ))
for r = 1 to len(s)-2
f = instr(0, @"_- ", mid(s, r, 1))
if f <> NSNotFound
s = fn stringwithformat(@"%@%@%@", left( s, r ), ucase(mid(s,r+1,1)), mid(s,r+2))
end if
next
end fn = s
 
local fn CamelTo_snake( s as CFStringRef ) as CFStringRef
long r,f
s = fn StringByTrimmingCharactersInSet( s, fn CFCharacterSetGetPredefined(_kCFCharacterSetWhitespace ))
s = fn StringByReplacingOccurrencesOfString( s, @" ", @"_")
s = fn StringByReplacingOccurrencesOfString( s, @"-", @"_")
for r = 1 to len(s)-2
f = instr(0, @"ABCDEFGHIJKLMNOPQRSTUVWXYZ", mid(s, r, 1))
if f <> NSNotFound
if fn StringIsEqual(@"_", mid(s, r-1, 1)) then continue
s = fn stringwithformat(@"%@%@%@", left( s, r ), @"_", mid(s,r))
end if
next
s = fn stringwithformat(@"%@%@",left( s, 1), lcase(mid(s, 1)))
end fn = s
 
local fn show( s1 as CFStringRef )
CFStringRef s = @" "
CFStringRef s2 = fn snake_toCamel( s1 )
CFStringRef s3 = fn CamelTo_snake( s1 )
nslog(@" \"%@\"%@\"%@\"%@\"%@\"", s1, mid(s, len(s1)), s2, mid(s, len(s2)), s3)
end fn
 
nslog( @"%@",@"String ¬
fn snake_toCamel ¬
fn CamelTo_snake")
fn show(@"snakeCase")
fn show(@"snake_case")
fn show(@"variable_10_case")
fn show(@"variable10Case")
fn show(@"ɛrgo rE tHis")
fn show(@"hurry-up-joe!")
fn show(@"c://my-docs/happy_Flag-Day/12.doc")
fn show(@" spaces ")
 
handleevents
</syntaxhighlight>
{{out}}
<syntaxhighlight lang="futurebasic">
String fn snake_toCamel fn CamelTo_snake
"snakeCase" "snakeCase" "snake_case"
"snake_case" "snakeCase" "snake_case"
"variable_10_case" "variable10Case" "variable_10_case"
"variable10Case" "variable10Case" "variable10_case"
"ɛrgo rE tHis" "ɛrgoRETHis" "ɛrgo_r_e_t_his"
"hurry-up-joe!" "hurryUpJoe!" "hurry_up_joe!"
" spaces " "spaces" "spaces"
"c://my-docs/happy_Flag-Day/12.doc" "c://myDocs/happyFlagDay/12.doc" "c://my_docs/happy_flag_day/12.doc"
</syntaxhighlight>
=={{header|Java}}==
The output of this example reflects the authors interpretation of the task.
Line 1,153 ⟶ 1,380:
spaces => spaces
</pre>
 
=={{header|Kotlin}}==
{{trans|Scala}}
<syntaxhighlight lang="Kotlin">
fun main() {
val variableNames = listOf("snakeCase", "snake_case", "variable_10_case", "variable10Case",
"ergo rE tHis", "hurry-up-joe!", "c://my-docs/happy_Flag-Day/12.doc", " spaces ")
 
println(" ".repeat(26) + "=== To snake_case ===")
variableNames.forEach { text ->
println("${text.padStart(34)} --> ${toSnakeCase(text)}")
}
 
println("\n" + " ".repeat(26) + "=== To camelCase ===")
variableNames.forEach { text ->
println("${text.padStart(34)} --> ${toCamelCase(text)}")
}
}
 
fun toSnakeCase(camel: String): String {
val snake = StringBuilder()
camel.trim().replace(" ", "_").replace("-", "_").forEach { ch ->
if (snake.isEmpty() || snake.last() != '_' || ch != '_') {
if (ch.isUpperCase() && snake.isNotEmpty() && snake.last() != '_') snake.append('_')
snake.append(ch.toLowerCase())
}
}
return snake.toString()
}
 
fun toCamelCase(snake: String): String {
val camel = StringBuilder()
var underscore = false
snake.trim().replace(" ", "_").replace("-", "_").forEach { ch ->
if (ch == '_') {
underscore = true
} else if (underscore) {
camel.append(ch.toUpperCase())
underscore = false
} else {
camel.append(ch)
}
}
return camel.toString()
}
</syntaxhighlight>
{{out}}
<pre>
=== To snake_case ===
snakeCase --> snake_case
snake_case --> snake_case
variable_10_case --> variable_10_case
variable10Case --> variable10_case
ergo rE tHis --> ergo_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 camelCase ===
snakeCase --> snakeCase
snake_case --> snakeCase
variable_10_case --> variable10Case
variable10Case --> variable10Case
ergo rE tHis --> ergoRETHis
hurry-up-joe! --> hurryUpJoe!
c://my-docs/happy_Flag-Day/12.doc --> c://myDocs/happyFlagDay/12.doc
spaces --> spaces
 
</pre>
 
 
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
Line 1,968 ⟶ 2,266:
c://my-docs/happy_Flag-Day/12.doc ==> c://my-docs/happy_Flag-Day/12.doc
spaces ==> spaces</pre>
 
 
=={{header|RPL}}==
« 1
'''WHILE''' DUP2 DUP SUB " " == '''REPEAT''' 1 + '''END'''
OVER DUP SIZE
'''WHILE''' DUP2 DUP SUB " " == '''REPEAT''' 1 - '''END'''
SWAP DROP SUB
» '<span style="color:blue">TRIM</span>' STO
« <span style="color:blue">TRIM</span> → s
« ""
1 s SIZE '''FOR''' j
s j DUP SUB
'''CASE'''
"- " OVER POS '''THEN'''
DROP "_" '''END'''
DUP "A" ≥ OVER "Z" ≤ AND '''THEN'''
NUM 32 + CHR
'''IF''' OVER DUP SIZE DUP SUB "_" ≠ '''THEN''' "_" SWAP + '''END'''
'''END'''
'''END'''
+
'''NEXT'''
» » '<span style="color:blue">→SNAKE</span>' STO
« <span style="color:blue">TRIM</span> → s
« "" 1 CF
1 s SIZE '''FOR''' j
s j DUP SUB
'''CASE'''
"-_ " OVER POS '''THEN'''
DROP "" 1 SF '''END'''
DUP "a" ≥ OVER "z" ≤ AND 1 FS?C AND '''THEN'''
NUM 32 - CHR '''END'''
'''END'''
+
'''NEXT'''
» » '<span style="color:blue">→CAMEL</span>' STO
« { "snakeCase" "snake_case" "variable_10_case" "variable10Case"
"εrgo rE tHis" "hurry-up-joe!" "c://my-docs/happy_Flag-Day/12.doc" " spaces " }
DUP 1 « <span style="color:blue">→SNAKE</span> » DOLIST
SWAP 1 « <span style="color:blue">→CAMEL</span> » DOLIST
» '<span style="color:blue">TASK</span>' STO
{{out}}
<pre>
2: { "snake_case" "snake_case" "variable_10_case" "variable10_case" "εrgo_r_e_t_his" "hurry_up_joe!" "c://my_docs/happy_flag_day/12.doc" "spaces" }
1: { "snakeCase" "snakeCase" "variable10Case" "variable10Case" "εrgoRETHis" "hurryUpJoe!" "c://myDocs/happyFlagDay/12.doc" "spaces" }
</pre>
 
=={{header|Scala}}==
{{trans|Java}}
<syntaxhighlight lang="Scala">
object CamelCaseAndSnakeCase extends App {
 
val variableNames = List("snakeCase", "snake_case", "variable_10_case", "variable10Case",
"ergo rE tHis", "hurry-up-joe!", "c://my-docs/happy_Flag-Day/12.doc", " spaces ")
 
println(" " * 26 + "=== To snake_case ===")
variableNames.foreach { text =>
println(f"$text%34s --> ${toSnakeCase(text)}")
}
 
println("\n" + " " * 26 + "=== To camelCase ===")
variableNames.foreach { text =>
println(f"$text%34s --> ${toCamelCase(text)}")
}
 
def toSnakeCase(camel: String): String = {
val snake = new StringBuilder
camel.trim.replace(" ", "_").replace("-", "_").foreach { ch =>
if (snake.isEmpty || snake.last != '_' || ch != '_') {
if (ch.isUpper && snake.nonEmpty && snake.last != '_') snake.append('_')
snake.append(ch.toLower)
}
}
snake.toString
}
 
def toCamelCase(snake: String): String = {
val camel = new StringBuilder
var underscore = false
snake.trim.replace(" ", "_").replace("-", "_").foreach { ch =>
if (ch == '_') underscore = true
else if (underscore) {
camel.append(ch.toUpper)
underscore = false
} else camel.append(ch)
}
camel.toString
}
}
</syntaxhighlight>
{{out}}
<pre>
=== To snake_case ===
snakeCase --> snake_case
snake_case --> snake_case
variable_10_case --> variable_10_case
variable10Case --> variable10_case
ergo rE tHis --> ergo_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 camelCase ===
snakeCase --> snakeCase
snake_case --> snakeCase
variable_10_case --> variable10Case
variable10Case --> variable10Case
ergo rE tHis --> ergoRETHis
hurry-up-joe! --> hurryUpJoe!
c://my-docs/happy_Flag-Day/12.doc --> c://myDocs/happyFlagDay/12.doc
spaces --> spaces
 
</pre>
 
=={{header|V (Vlang)}}==
{{trans|go}}
Line 2,029 ⟶ 2,445:
 
2. I've assumed that an underscore should not be added if the previous character was already a separator.
<syntaxhighlight lang="ecmascriptwren">import "./str" for Char
import "./fmt" for Fmt
 
var toCamel = Fn.new { |snake|
Line 2,107 ⟶ 2,523:
spaces -> spaces
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">string 0; \use zero-terminated strings
1,969

edits