builtin: vfmt every .v file, except vlib/builtin/int_test.v (#9448)

pull/9445/head
zakuro 2021-03-25 03:39:59 +09:00 committed by GitHub
parent 5d8b9b0151
commit 6bc9ef7373
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 259 additions and 272 deletions

View File

@ -17,6 +17,7 @@ const (
'examples/term.ui', 'examples/term.ui',
] ]
verify_known_failing_exceptions = [ verify_known_failing_exceptions = [
'vlib/builtin/int_test.v' /* special number formatting that should be tested */,
'vlib/gg/m4/graphic.v' /* has hand crafted meaningful formatting of matrices */, 'vlib/gg/m4/graphic.v' /* has hand crafted meaningful formatting of matrices */,
'vlib/gg/m4/m4_test.v' /* has hand crafted meaningful formatting of matrices */, 'vlib/gg/m4/m4_test.v' /* has hand crafted meaningful formatting of matrices */,
'vlib/gg/m4/matrix.v' /* has hand crafted meaningful formatting of matrices */, 'vlib/gg/m4/matrix.v' /* has hand crafted meaningful formatting of matrices */,
@ -28,12 +29,7 @@ const (
'vlib/arrays/', 'vlib/arrays/',
'vlib/benchmark/', 'vlib/benchmark/',
'vlib/bitfield/', 'vlib/bitfield/',
'vlib/builtin/array.v', 'vlib/builtin/',
'vlib/builtin/array_test.v',
'vlib/builtin/string.v',
'vlib/builtin/map.v',
'vlib/builtin/int.v',
'vlib/builtin/option.v',
'vlib/cli/', 'vlib/cli/',
'vlib/dl/', 'vlib/dl/',
'vlib/flag/', 'vlib/flag/',
@ -72,7 +68,6 @@ const (
'vlib/v/vmod/', 'vlib/v/vmod/',
'vlib/cli/', 'vlib/cli/',
'vlib/flag/', 'vlib/flag/',
'vlib/gg/gg.v',
'vlib/math/big/', 'vlib/math/big/',
'vlib/os/', 'vlib/os/',
'vlib/semver/', 'vlib/semver/',

View File

@ -35,7 +35,6 @@ fn (mut a array) set(i int, val voidptr) {
mem_copy(a.data + a.element_size * i, val, a.element_size) mem_copy(a.data + a.element_size * i, val, a.element_size)
} }
// array.repeat returns new array with the given array elements // array.repeat returns new array with the given array elements
// repeated `nr_repeat` times // repeated `nr_repeat` times
pub fn (a array) repeat(nr_repeats int) array { pub fn (a array) repeat(nr_repeats int) array {

View File

@ -14,7 +14,7 @@ pub fn print(s string) {
pub fn println(s string) { pub fn println(s string) {
print(s) print(s)
print("\n") print('\n')
} }
pub fn panic(s string) { pub fn panic(s string) {
@ -24,7 +24,7 @@ pub fn panic(s string) {
} }
// replaces panic when -debug arg is passed // replaces panic when -debug arg is passed
fn panic_debug(line_no int, file, mod, fn_name, s string) { fn panic_debug(line_no int, file string, mod string, fn_name string, s string) {
eprintln('================ V panic ================') eprintln('================ V panic ================')
eprint(' module: ') eprint(' module: ')
eprintln('mod') eprintln('mod')
@ -39,6 +39,7 @@ fn panic_debug(line_no int, file, mod, fn_name, s string) {
eprintln('=========================================') eprintln('=========================================')
sys_exit(1) sys_exit(1)
} }
pub fn eprint(s string) { pub fn eprint(s string) {
if isnil(s.str) { if isnil(s.str) {
panic('eprint(NIL)') panic('eprint(NIL)')
@ -48,7 +49,7 @@ pub fn eprint(s string) {
pub fn eprint_ln(s string) { pub fn eprint_ln(s string) {
eprint(s) eprint(s)
eprint("\n") eprint('\n')
} }
pub fn eprintln(s string) { pub fn eprintln(s string) {

View File

@ -257,6 +257,7 @@ fn sys_call2(scn u64, arg1 u64, arg2 u64) u64 {
} }
return res return res
} }
fn sys_call3(scn u64, arg1 u64, arg2 u64, arg3 u64) u64 { fn sys_call3(scn u64, arg1 u64, arg2 u64, arg3 u64) u64 {
res := u64(0) res := u64(0)
asm amd64 { asm amd64 {
@ -269,6 +270,7 @@ fn sys_call3(scn u64, arg1 u64, arg2 u64, arg3 u64) u64 {
} }
return res return res
} }
fn sys_call4(scn u64, arg1 u64, arg2 u64, arg3 u64, arg4 u64) u64 { fn sys_call4(scn u64, arg1 u64, arg2 u64, arg3 u64, arg4 u64) u64 {
res := u64(0) res := u64(0)
asm amd64 { asm amd64 {
@ -298,10 +300,12 @@ fn sys_call5(scn u64, arg1 u64, arg2 u64, arg3 u64, arg4 u64, arg5 u64) u64 {
d (arg3) d (arg3)
r (arg4) r (arg4)
r (arg5) r (arg5)
; r10 r8 ; r10
r8
} }
return res return res
} }
fn sys_call6(scn u64, arg1 u64, arg2 u64, arg3 u64, arg4 u64, arg5 u64, arg6 u64) u64 { fn sys_call6(scn u64, arg1 u64, arg2 u64, arg3 u64, arg4 u64, arg5 u64, arg6 u64) u64 {
res := u64(0) res := u64(0)
asm amd64 { asm amd64 {
@ -317,7 +321,9 @@ fn sys_call6(scn u64, arg1 u64, arg2 u64, arg3 u64, arg4 u64, arg5 u64, arg6 u64
r (arg4) r (arg4)
r (arg5) r (arg5)
r (arg6) r (arg6)
; r10 r8 r9 ; r10
r8
r9
} }
return res return res
} }

View File

@ -69,40 +69,56 @@ pub fn tos3(s charptr) string {
} }
} }
pub fn string_eq (s1, s2 string) bool { pub fn string_eq(s1 string, s2 string) bool {
if s1.len != s2.len { return false } if s1.len != s2.len {
return false
}
for i in 0 .. s1.len { for i in 0 .. s1.len {
if s1[i] != s2[i] { return false } if s1[i] != s2[i] {
return false
}
} }
return true return true
} }
pub fn string_ne (s1, s2 string) bool {
pub fn string_ne(s1 string, s2 string) bool {
return !string_eq(s1, s2) return !string_eq(s1, s2)
} }
pub fn i64_tos(buf byteptr, len int, n0 i64, base int) string { pub fn i64_tos(buf byteptr, len int, n0 i64, base int) string {
if base < 2 { panic("base must be >= 2")} if base < 2 {
if base > 36 { panic("base must be <= 36")} panic('base must be >= 2')
}
if base > 36 {
panic('base must be <= 36')
}
mut b := tos(buf, len) mut b := tos(buf, len)
mut i := len - 1 mut i := len - 1
mut n := n0 mut n := n0
neg := n < 0 neg := n < 0
if neg { n = -n } if neg {
n = -n
}
b[i--] = 0 b[i--] = 0
for { for {
c := (n % base) + 48 c := (n % base) + 48
b[i--] = if c > 57 { c + 7 } else { c } b[i--] = if c > 57 { c + 7 } else { c }
if i < 0 { panic ("buffer to small") } if i < 0 {
panic('buffer to small')
}
n /= base n /= base
if n < 1 {break} if n < 1 {
break
}
} }
if neg { if neg {
if i < 0 { panic ("buffer to small") } if i < 0 {
panic('buffer to small')
}
b[i--] = 45 b[i--] = 45
} }
offset := i + 1 offset := i + 1
@ -117,7 +133,7 @@ pub fn i64_str(n0 i64, base int) string {
} }
pub fn ptr_str(ptr voidptr) string { pub fn ptr_str(ptr voidptr) string {
buf := [16]byte buf := [16]byte{}
hex := i64_tos(buf, 15, i64(ptr), 16) hex := i64_tos(buf, 15, i64(ptr), 16)
res := '0x' + hex res := '0x' + hex
return res return res

View File

@ -16,7 +16,6 @@ const (
stdout_value = 1 stdout_value = 1
stderr_value = 2 stderr_value = 2
) )
*/ */
fn builtin_init() { fn builtin_init() {
@ -87,7 +86,7 @@ fn print_backtrace_skipping_top_frames_linux(skipframes int) bool {
return false return false
} $else { } $else {
$if tinyc { $if tinyc {
C.tcc_backtrace("Backtrace") C.tcc_backtrace('Backtrace')
return false return false
} }
buffer := [100]voidptr{} buffer := [100]voidptr{}

View File

@ -88,66 +88,42 @@ pub fn (x f32) strlong() string {
// Example: assert f32_abs(-2.0) == 2.0 // Example: assert f32_abs(-2.0) == 2.0
[inline] [inline]
pub fn f32_abs(a f32) f32 { pub fn f32_abs(a f32) f32 {
return if a < 0 { return if a < 0 { -a } else { a }
-a
} else {
a
}
} }
// f64_abs returns the absolute value of `a` as a `f64` value. // f64_abs returns the absolute value of `a` as a `f64` value.
// Example: assert f64_abs(-2.0) == f64(2.0) // Example: assert f64_abs(-2.0) == f64(2.0)
[inline] [inline]
fn f64_abs(a f64) f64 { fn f64_abs(a f64) f64 {
return if a < 0 { return if a < 0 { -a } else { a }
-a
} else {
a
}
} }
// f32_max returns the largest `f32` of input `a` and `b`. // f32_max returns the largest `f32` of input `a` and `b`.
// Example: assert f32_max(2.0,3.0) == 3.0 // Example: assert f32_max(2.0,3.0) == 3.0
[inline] [inline]
pub fn f32_max(a f32, b f32) f32 { pub fn f32_max(a f32, b f32) f32 {
return if a > b { return if a > b { a } else { b }
a
} else {
b
}
} }
// f32_min returns the smallest `f32` of input `a` and `b`. // f32_min returns the smallest `f32` of input `a` and `b`.
// Example: assert f32_min(2.0,3.0) == 2.0 // Example: assert f32_min(2.0,3.0) == 2.0
[inline] [inline]
pub fn f32_min(a f32, b f32) f32 { pub fn f32_min(a f32, b f32) f32 {
return if a < b { return if a < b { a } else { b }
a
} else {
b
}
} }
// f64_max returns the largest `f64` of input `a` and `b`. // f64_max returns the largest `f64` of input `a` and `b`.
// Example: assert f64_max(2.0,3.0) == 3.0 // Example: assert f64_max(2.0,3.0) == 3.0
[inline] [inline]
pub fn f64_max(a f64, b f64) f64 { pub fn f64_max(a f64, b f64) f64 {
return if a > b { return if a > b { a } else { b }
a
} else {
b
}
} }
// f64_min returns the smallest `f64` of input `a` and `b`. // f64_min returns the smallest `f64` of input `a` and `b`.
// Example: assert f64_min(2.0,3.0) == 2.0 // Example: assert f64_min(2.0,3.0) == 2.0
[inline] [inline]
fn f64_min(a f64, b f64) f64 { fn f64_min(a f64, b f64) f64 {
return if a < b { return if a < b { a } else { b }
a
} else {
b
}
} }
// eq_epsilon returns true if the `f32` is equal to input `b`. // eq_epsilon returns true if the `f32` is equal to input `b`.

View File

@ -74,7 +74,7 @@ pub fn (o Option) str() string {
if o.state == 1 { if o.state == 1 {
return 'Option{ none }' return 'Option{ none }'
} }
return 'Option{ error: "${o.err}" }' return 'Option{ error: "$o.err" }'
} }
pub fn error(s string) Option { pub fn error(s string) Option {
@ -94,5 +94,4 @@ pub fn error_with_code(s string, code int) Option {
code: code code: code
} }
} }
} }

View File

@ -8,13 +8,17 @@
module builtin module builtin
pub struct JS.Number {} pub struct JS.Number {}
pub struct JS.String { pub struct JS.String {
length JS.Number length JS.Number
} }
pub struct JS.Boolean {} pub struct JS.Boolean {}
pub struct JS.Array { pub struct JS.Array {
length JS.Number length JS.Number
} }
pub struct JS.Map {} pub struct JS.Map {}
// Type prototype functions // Type prototype functions
@ -29,6 +33,7 @@ fn (v JS.Map) toString() JS.String
fn native_str_arr_len(arr []JS.String) int { fn native_str_arr_len(arr []JS.String) int {
len := 0 len := 0
#len = arr.length #len = arr.length
return len return len
} }
@ -41,7 +46,9 @@ fn JS.isFinite(f64) bool
fn JS.decodeURI(string) string fn JS.decodeURI(string) string
fn JS.decodeURIComponent(string) string fn JS.decodeURIComponent(string) string
fn JS.encodeURI(string) string fn JS.encodeURI(string) string
type EncodeURIComponentArg = string | f64 | bool
type EncodeURIComponentArg = bool | f64 | string
fn JS.encodeURIComponent(EncodeURIComponentArg) string fn JS.encodeURIComponent(EncodeURIComponentArg) string
fn JS.escape(string) string fn JS.escape(string) string
fn JS.unescape(string) string fn JS.unescape(string) string

View File

@ -28,8 +28,10 @@ fn JS.blur()
fn JS.captureEvents() fn JS.captureEvents()
fn JS.close() fn JS.close()
fn JS.confirm(string) bool fn JS.confirm(string) bool
// fn JS.departFocus(NavigationReason, FocusNavigationOrigin) // fn JS.departFocus(NavigationReason, FocusNavigationOrigin)
fn JS.focus() fn JS.focus()
// fn JS.getComputedStyle(Element, string | null) CSSStyleDeclaration // fn JS.getComputedStyle(Element, string | null) CSSStyleDeclaration
// fn JS.getMatchedCSSRules(Element, string | null) CSSRuleList // fn JS.getMatchedCSSRules(Element, string | null) CSSRuleList
// fn JS.getSelection() Selection | null // fn JS.getSelection() Selection | null
@ -37,6 +39,7 @@ fn JS.focus()
fn JS.moveBy(int, int) fn JS.moveBy(int, int)
fn JS.moveTo(int, int) fn JS.moveTo(int, int)
fn JS.msWriteProfilerMark(string) fn JS.msWriteProfilerMark(string)
// fn JS.open(string, string, string, bool) ?Window // fn JS.open(string, string, string, bool) ?Window
// fn JS.postMessage(any, string, []Transferable) // fn JS.postMessage(any, string, []Transferable)
fn JS.print() fn JS.print()
@ -44,10 +47,13 @@ fn JS.prompt(string, string) ?string
fn JS.releaseEvents() fn JS.releaseEvents()
fn JS.resizeBy(int, int) fn JS.resizeBy(int, int)
fn JS.resizeTo(int, int) fn JS.resizeTo(int, int)
// fn JS.scroll(ScrollToOptions) // fn JS.scroll(ScrollToOptions)
fn JS.scroll(int, int) fn JS.scroll(int, int)
// fn JS.scrollBy(ScrollToOptions) // fn JS.scrollBy(ScrollToOptions)
fn JS.scrollBy(int, int) fn JS.scrollBy(int, int)
// fn JS.scrollTo(ScrollToOptions) // fn JS.scrollTo(ScrollToOptions)
fn JS.scrollTo(int, int) fn JS.scrollTo(int, int)
fn JS.stop() fn JS.stop()

View File

@ -14,7 +14,6 @@ pub fn (s string) after(dot string) string {
return string(s.str.slice(s.str.lastIndexOf(dot.str) + 1, int(s.str.length))) return string(s.str.slice(s.str.lastIndexOf(dot.str) + 1, int(s.str.length)))
} }
pub fn (s string) after_char(dot byte) string { pub fn (s string) after_char(dot byte) string {
// TODO: Implement after byte // TODO: Implement after byte
return s return s

View File

@ -91,7 +91,9 @@ fn (mut m SortedMap) set(key string, value voidptr) {
} }
} }
mut i := 0 mut i := 0
for i < node.len && key > node.keys[i] { i++ } for i < node.len && key > node.keys[i] {
i++
}
if i != node.len && key == node.keys[i] { if i != node.len && key == node.keys[i] {
unsafe { unsafe {
C.memcpy(node.values[i], value, m.value_bytes) C.memcpy(node.values[i], value, m.value_bytes)
@ -162,7 +164,9 @@ fn (m SortedMap) get(key string, out voidptr) bool {
mut node := m.root mut node := m.root
for { for {
mut i := node.len - 1 mut i := node.len - 1
for i >= 0 && key < node.keys[i] { i-- } for i >= 0 && key < node.keys[i] {
i--
}
if i != -1 && key == node.keys[i] { if i != -1 && key == node.keys[i] {
unsafe { unsafe {
C.memcpy(out, node.values[i], m.value_bytes) C.memcpy(out, node.values[i], m.value_bytes)
@ -184,7 +188,9 @@ fn (m SortedMap) exists(key string) bool {
mut node := m.root mut node := m.root
for { for {
mut i := node.len - 1 mut i := node.len - 1
for i >= 0 && key < node.keys[i] { i-- } for i >= 0 && key < node.keys[i] {
i--
}
if i != -1 && key == node.keys[i] { if i != -1 && key == node.keys[i] {
return true return true
} }

View File

@ -1,7 +1,7 @@
fn test_common_atoi() { fn test_common_atoi() {
// test common cases // test common cases
assert "70zzz".int() == 70 assert '70zzz'.int() == 70
assert "2901issue".int() == 2901 assert '2901issue'.int() == 2901
assert '234232w'.int() == 234232 assert '234232w'.int() == 234232
assert '-9009x'.int() == -9009 assert '-9009x'.int() == -9009
assert '0y'.int() == 0 assert '0y'.int() == 0
@ -21,7 +21,7 @@ fn test_common_atoi() {
assert '10_000_000'.int() == 10000000 assert '10_000_000'.int() == 10000000
for n in -10000 .. 100000 { for n in -10000 .. 100000 {
s := n.str()+"z" s := n.str() + 'z'
assert s.int() == n assert s.int() == n
} }
} }
@ -30,8 +30,8 @@ fn test_unsigned_cast() {
// tests for u16 // tests for u16
// test common cases // test common cases
assert "70zzz".u16() == 70 assert '70zzz'.u16() == 70
assert "2901issue".u16() == 2901 assert '2901issue'.u16() == 2901
assert '0y'.u16() == 0 assert '0y'.u16() == 0
// test lead zeros // test lead zeros
@ -44,8 +44,8 @@ fn test_unsigned_cast() {
// tests for u32 // tests for u32
// test common cases // test common cases
assert "70zzz".u32() == 70 assert '70zzz'.u32() == 70
assert "2901issue".u32() == 2901 assert '2901issue'.u32() == 2901
assert '234232w'.u32() == 234232 assert '234232w'.u32() == 234232
assert '-9009x'.u32() == 0 assert '-9009x'.u32() == 0
assert '0y'.u32() == 0 assert '0y'.u32() == 0
@ -65,15 +65,15 @@ fn test_unsigned_cast() {
assert '10_000_000'.u32() == 10000000 assert '10_000_000'.u32() == 10000000
for n in 0 .. 100 { for n in 0 .. 100 {
s := n.str()+"z" s := n.str() + 'z'
assert s.u32() == n assert s.u32() == n
} }
// tests for u64 // tests for u64
// test common cases // test common cases
assert "70zzz".u64() == 70 assert '70zzz'.u64() == 70
assert "2901issue".u64() == 2901 assert '2901issue'.u64() == 2901
assert '234232w'.u64() == 234232 assert '234232w'.u64() == 234232
assert '-9009x'.u64() == 0 assert '-9009x'.u64() == 0
assert '0y'.u64() == 0 assert '0y'.u64() == 0
@ -93,18 +93,17 @@ fn test_unsigned_cast() {
assert '10_000_000'.u64() == 10000000 assert '10_000_000'.u64() == 10000000
for n in 0 .. 10000 { for n in 0 .. 10000 {
s := n.str()+"z" s := n.str() + 'z'
assert s.u64() == n assert s.u64() == n
} }
} }
fn test_signed_cast() { fn test_signed_cast() {
// tests for i64 // tests for i64
// test common cases // test common cases
assert "70zzz".i64() == 70 assert '70zzz'.i64() == 70
assert "2901issue".i64() == 2901 assert '2901issue'.i64() == 2901
assert '234232w'.i64() == 234232 assert '234232w'.i64() == 234232
assert '-9009x'.i64() == -9009 assert '-9009x'.i64() == -9009
assert '0y'.i64() == 0 assert '0y'.i64() == 0
@ -124,15 +123,15 @@ fn test_signed_cast() {
assert '10_000_000'.i64() == 10000000 assert '10_000_000'.i64() == 10000000
for n in -10000 .. 100000 { for n in -10000 .. 100000 {
s := n.str()+"z" s := n.str() + 'z'
assert s.i64() == n assert s.i64() == n
} }
// tests for i8 // tests for i8
// test common cases // test common cases
assert "70zzz".i8() == 70 assert '70zzz'.i8() == 70
assert "29issue".i8() == 29 assert '29issue'.i8() == 29
assert '22w'.i8() == 22 assert '22w'.i8() == 22
assert '-90x'.i8() == -90 assert '-90x'.i8() == -90
assert '0y'.i8() == 0 assert '0y'.i8() == 0
@ -152,15 +151,15 @@ fn test_signed_cast() {
assert '10_0'.i8() == 100 assert '10_0'.i8() == 100
for n in -10 .. 100 { for n in -10 .. 100 {
s := n.str()+"z" s := n.str() + 'z'
assert s.i8() == n assert s.i8() == n
} }
// tests for i16 // tests for i16
// test common cases // test common cases
assert "70zzz".i16() == 70 assert '70zzz'.i16() == 70
assert "2901issue".i16() == 2901 assert '2901issue'.i16() == 2901
assert '2342w'.i16() == 2342 assert '2342w'.i16() == 2342
assert '-9009x'.i16() == -9009 assert '-9009x'.i16() == -9009
assert '0y'.i16() == 0 assert '0y'.i16() == 0
@ -180,7 +179,7 @@ fn test_signed_cast() {
assert '10_0'.i16() == 100 assert '10_0'.i16() == 100
for n in -100 .. 100 { for n in -100 .. 100 {
s := n.str()+"z" s := n.str() + 'z'
assert s.i16() == n assert s.i16() == n
} }
} }

View File

@ -3,10 +3,7 @@
// that can be found in the LICENSE file. // that can be found in the LICENSE file.
fn test_strip_margins_no_tabs() { fn test_strip_margins_no_tabs() {
no_tabs := ['Hello there', no_tabs := ['Hello there', 'This is a string', 'With multiple lines'].join('\n')
'This is a string',
'With multiple lines',
].join('\n')
no_tabs_stripped := 'Hello there no_tabs_stripped := 'Hello there
|This is a string |This is a string
|With multiple lines'.strip_margin() |With multiple lines'.strip_margin()
@ -14,10 +11,7 @@ fn test_strip_margins_no_tabs() {
} }
fn test_strip_margins_text_before() { fn test_strip_margins_text_before() {
text_before := ['There is text', text_before := ['There is text', 'before the delimiter', 'that should be removed as well'].join('\n')
'before the delimiter',
'that should be removed as well',
].join('\n')
text_before_stripped := 'There is text text_before_stripped := 'There is text
f lasj asldfj j lksjdf |before the delimiter f lasj asldfj j lksjdf |before the delimiter
Which is removed hello |that should be removed as well'.strip_margin() Which is removed hello |that should be removed as well'.strip_margin()
@ -25,10 +19,7 @@ fn test_strip_margins_text_before() {
} }
fn test_strip_margins_white_space_after_delim() { fn test_strip_margins_white_space_after_delim() {
tabs := [' Tab', tabs := [' Tab', ' spaces', ' another tab'].join('\n')
' spaces',
' another tab',
].join('\n')
tabs_stripped := ' Tab tabs_stripped := ' Tab
| spaces | spaces
| another tab'.strip_margin() | another tab'.strip_margin()
@ -36,8 +27,7 @@ fn test_strip_margins_white_space_after_delim() {
} }
fn test_strip_margins_alternate_delim() { fn test_strip_margins_alternate_delim() {
alternate_delimiter := ['This has a different delim,', alternate_delimiter := ['This has a different delim,', 'but that is ok',
'but that is ok',
'because everything works', 'because everything works',
].join('\n') ].join('\n')
alternate_delimiter_stripped := 'This has a different delim, alternate_delimiter_stripped := 'This has a different delim,
@ -48,9 +38,7 @@ fn test_strip_margins_alternate_delim() {
fn test_strip_margins_multiple_delims_after_first() { fn test_strip_margins_multiple_delims_after_first() {
delim_after_first_instance := ['The delimiter used', delim_after_first_instance := ['The delimiter used',
'only matters the |||| First time it is seen', 'only matters the |||| First time it is seen', 'not any | other | times'].join('\n')
'not any | other | times',
].join('\n')
delim_after_first_instance_stripped := 'The delimiter used delim_after_first_instance_stripped := 'The delimiter used
|only matters the |||| First time it is seen |only matters the |||| First time it is seen
|not any | other | times'.strip_margin() |not any | other | times'.strip_margin()
@ -58,15 +46,14 @@ fn test_strip_margins_multiple_delims_after_first() {
} }
fn test_strip_margins_uneven_delims() { fn test_strip_margins_uneven_delims() {
uneven_delims := ['It doesn\'t matter if the delims are uneven,', uneven_delims := ["It doesn't matter if the delims are uneven,",
'The text will still be delimited correctly.', 'The text will still be delimited correctly.', 'Maybe not everything needs 3 lines?',
'Maybe not everything needs 3 lines?',
'Let us go for 4 then', 'Let us go for 4 then',
].join('\n') ].join('\n')
uneven_delims_stripped := 'It doesn\'t matter if the delims are uneven, uneven_delims_stripped := "It doesn't matter if the delims are uneven,
|The text will still be delimited correctly. |The text will still be delimited correctly.
|Maybe not everything needs 3 lines? |Maybe not everything needs 3 lines?
|Let us go for 4 then'.strip_margin() |Let us go for 4 then".strip_margin()
assert uneven_delims_stripped == uneven_delims assert uneven_delims_stripped == uneven_delims
} }
@ -83,10 +70,7 @@ fn test_strip_margins_multiple_blank_lines() {
} }
fn test_strip_margins_end_newline() { fn test_strip_margins_end_newline() {
end_with_newline := ['This line will end with a newline', end_with_newline := ['This line will end with a newline', 'Something cool or something.', ''].join('\n')
'Something cool or something.',
'',
].join('\n')
end_with_newline_stripped := 'This line will end with a newline end_with_newline_stripped := 'This line will end with a newline
|Something cool or something. |Something cool or something.
@ -95,22 +79,17 @@ fn test_strip_margins_end_newline() {
} }
fn test_strip_margins_space_delimiter() { fn test_strip_margins_space_delimiter() {
space_delimiter := ['Using a white-space char will', space_delimiter := ['Using a white-space char will', 'revert back to default behavior.'].join('\n')
'revert back to default behavior.',
].join('\n')
space_delimiter_stripped := 'Using a white-space char will space_delimiter_stripped := 'Using a white-space char will
|revert back to default behavior.'.strip_margin_custom(`\n`) |revert back to default behavior.'.strip_margin_custom(`\n`)
assert space_delimiter == space_delimiter_stripped assert space_delimiter == space_delimiter_stripped
} }
fn test_strip_margins_crlf() { fn test_strip_margins_crlf() {
crlf := ['This string\'s line endings have CR as well as LFs.', crlf := ["This string's line endings have CR as well as LFs.", 'This should pass', 'Definitely'].join('\r\n')
'This should pass', crlf_stripped := "This string's line endings have CR as well as LFs.\r
'Definitely',
].join('\r\n')
crlf_stripped := 'This string\'s line endings have CR as well as LFs.\r
|This should pass\r |This should pass\r
|Definitely'.strip_margin() |Definitely".strip_margin()
assert crlf == crlf_stripped assert crlf == crlf_stripped
} }

View File

@ -7,8 +7,8 @@ const (
pub fn (_str string) to_wide() &u16 { pub fn (_str string) to_wide() &u16 {
$if windows { $if windows {
unsafe { unsafe {
num_chars := (C.MultiByteToWideChar(cp_utf8, 0, charptr(_str.str), _str.len, 0, num_chars := (C.MultiByteToWideChar(cp_utf8, 0, charptr(_str.str), _str.len,
0)) 0, 0))
mut wstr := &u16(malloc((num_chars + 1) * 2)) // sizeof(wchar_t) mut wstr := &u16(malloc((num_chars + 1) * 2)) // sizeof(wchar_t)
if wstr != 0 { if wstr != 0 {
C.MultiByteToWideChar(cp_utf8, 0, charptr(_str.str), _str.len, wstr, num_chars) C.MultiByteToWideChar(cp_utf8, 0, charptr(_str.str), _str.len, wstr, num_chars)