-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add unmarshal_to to modify an existing object
- Loading branch information
Showing
16 changed files
with
264 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
.DS_Store | ||
unmarshal_test | ||
unmarshal_test.dSYM | ||
*_test | ||
*.dSYM |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,8 @@ | ||
{ | ||
"files.exclude": { | ||
"**/.git": true, | ||
"**/.DS_Store": true | ||
}, | ||
"search.exclude": { | ||
"**/node_modules": true | ||
"**/.DS_Store": true, | ||
"**/*_test": true, | ||
"**/*.dSYM": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
all: check test | ||
|
||
check: | ||
v fmt -w . | ||
v vet . | ||
|
||
test: | ||
NO_COLOR=1 v test . | ||
|
||
clean: | ||
rm -rf src/*_test src/*.dSYM | ||
|
||
version: | ||
npx conventional-changelog-cli -p angular -i CHANGELOG.md -s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#!/usr/bin/env -S v -prod run | ||
|
||
import benchmark | ||
import rand | ||
import strconv | ||
import time | ||
|
||
fn C.strtod(charptr, &charptr) f64 | ||
fn C.strtol(charptr, &charptr, int) int | ||
|
||
fn strtod(s string) !f64 { | ||
end := unsafe { nil } | ||
C.errno = 0 | ||
n := C.strtod(s.str, &end) | ||
if C.errno != 0 { | ||
return error('out of range') | ||
} | ||
if unsafe { s.str + s.len != end } { | ||
return error('not a number') | ||
} | ||
return n | ||
} | ||
|
||
fn strtol(s string) !int { | ||
end := unsafe { nil } | ||
C.errno = 0 | ||
n := C.strtol(s.str, &end, 10) | ||
if C.errno != 0 { | ||
return error('out of range') | ||
} | ||
if unsafe { s.str + s.len != end } { | ||
return error('not a number') | ||
} | ||
return n | ||
} | ||
|
||
fn str_to_num(s string) !f64 { | ||
if s.contains_any('.eE') { | ||
return strtod(s)! | ||
} | ||
return f64(strconv.atoi(s) or { strtod(s)! }) | ||
} | ||
|
||
rand.seed([u32(time.now().unix), 0]) | ||
|
||
sample_size := 1500 | ||
mut strings := []string{} | ||
for _ in 0 .. sample_size { | ||
n := rand.int_in_range(0, 10000000) or { 0 } | ||
strings << '${n}' | ||
d := rand.int_in_range(0, 10000000) or { 0 } | ||
f := rand.int_in_range(0, 10000000) or { 0 } | ||
strings << '${d}.${f}' | ||
} | ||
|
||
mut b := benchmark.start() | ||
|
||
for s in strings { | ||
_ := strconv.atof64(s)! | ||
} | ||
b.measure('parsing floats') | ||
|
||
for s in strings { | ||
_ := strtod(s)! | ||
} | ||
b.measure('parsing floats with C') | ||
|
||
for s in strings { | ||
_ := f64(strconv.atoi(s) or { strtod(s)! }) | ||
} | ||
b.measure('parsing integers and floats') | ||
|
||
for s in strings { | ||
_ := str_to_num(s)! | ||
} | ||
b.measure('parsing integers and floats with check') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#!/usr/bin/env -S v -prod run | ||
|
||
import benchmark | ||
import rand | ||
import strconv | ||
import time | ||
|
||
fn C.strtod(charptr, &charptr) f64 | ||
fn C.strtol(charptr, &charptr, int) int | ||
|
||
fn strtod(s string) !f64 { | ||
C.errno = 0 | ||
n := C.strtod(s.str, 0) | ||
if C.errno != 0 { | ||
return error('range overflow') | ||
} | ||
return n | ||
} | ||
|
||
fn strtol(s string) !int { | ||
C.errno = 0 | ||
n := C.strtol(s.str, 0, 10) | ||
if C.errno != 0 { | ||
return error('range overflow') | ||
} | ||
return n | ||
} | ||
|
||
rand.seed([u32(time.now().unix), 0]) | ||
|
||
sample_size := 3000 | ||
mut strings := []string{} | ||
for _ in 0 .. sample_size { | ||
n := rand.int_in_range(0, 10000000) or { 0 } | ||
strings << '${n}' | ||
} | ||
|
||
mut b := benchmark.start() | ||
for s in strings { | ||
_ := f64(strconv.atoi(s)!) | ||
} | ||
b.measure('parsing integers') | ||
|
||
for s in strings { | ||
C.errno = 0 | ||
_ := f64(C.strtol(s.str, 0, 10)) | ||
if C.errno != 0 { | ||
panic('range overflow') | ||
} | ||
} | ||
b.measure('parsing integers with C') | ||
|
||
for s in strings { | ||
_ := f64(strtol(s)!) | ||
} | ||
b.measure('parsing integers with C wrapped') | ||
|
||
for s in strings { | ||
_ := strconv.atof64(s)! | ||
} | ||
b.measure('parsing floats') | ||
|
||
for s in strings { | ||
_ := strconv.atof_quick(s) | ||
} | ||
b.measure('parsing floats quickly') | ||
|
||
for s in strings { | ||
C.errno = 0 | ||
_ := C.strtod(s.str, 0) | ||
if C.errno != 0 { | ||
panic('range overflow') | ||
} | ||
} | ||
b.measure('parsing floats with C') | ||
|
||
for s in strings { | ||
_ := strtod(s)! | ||
} | ||
b.measure('parsing floats with C wrapped') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ module yaml | |
|
||
import prantlf.jany { Any } | ||
|
||
[noinit] | ||
pub struct YamlError { | ||
Error | ||
message string | ||
|
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module yaml | ||
|
||
import prantlf.jany { UnmarshalOpts } | ||
|
||
pub fn unmarshal_text[T](input string, opts &UnmarshalOpts) !T { | ||
mut obj := T{} | ||
unmarshal_text_to[T](input, mut obj, opts)! | ||
return obj | ||
} | ||
|
||
pub fn unmarshal_text_to[T](input string, mut obj T, opts &UnmarshalOpts) ! { | ||
a := parse_text(input)! | ||
jany.unmarshal_to[T](a, mut obj, opts)! | ||
} | ||
|
||
pub fn unmarshal_file[T](path string, opts &UnmarshalOpts) !T { | ||
mut obj := T{} | ||
unmarshal_file_to[T](path, mut obj, opts)! | ||
return obj | ||
} | ||
|
||
pub fn unmarshal_file_to[T](path string, mut obj T, opts &UnmarshalOpts) ! { | ||
a := parse_file(path)! | ||
jany.unmarshal_to[T](a, mut obj, opts)! | ||
} |
File renamed without changes.
File renamed without changes.
This file was deleted.
Oops, something went wrong.