sample-code: Add a super hacky format-string implementation using eval and fmt builtins
This commit is contained in:
32
sample-code/fstring.cl
Normal file
32
sample-code/fstring.cl
Normal file
@@ -0,0 +1,32 @@
|
||||
|
||||
fn f(__fmt: str) -> str {
|
||||
let __out = "";
|
||||
let __expr = "";
|
||||
let __depth = 0;
|
||||
for __c in chars(__fmt) {
|
||||
match __c {
|
||||
'{' => {
|
||||
__depth += 1
|
||||
if __depth <= 1 {
|
||||
continue
|
||||
}
|
||||
},
|
||||
'}' => {
|
||||
__depth -= 1
|
||||
if __depth <= 0 {
|
||||
__out += fmt(eval(__expr))
|
||||
__expr = ""
|
||||
continue
|
||||
}
|
||||
},
|
||||
':' => if __depth == 1 {
|
||||
__out += __expr + ": "
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
if __depth > 0 {
|
||||
__expr += __c
|
||||
} else __out += __c;
|
||||
}
|
||||
__out
|
||||
}
|
||||
Reference in New Issue
Block a user