From 7e35d4066166982847199cddefbfb00d129b8b89 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Thu, 3 Jul 2025 09:46:01 +0300 Subject: [PATCH] v2: add formatting workarounds, to make `v run cmd/v2/v2.v --skip-imports -d vlib/v2/tests/syntax.v_` run --- vlib/builtin/rune_map.v | 3 +-- vlib/builtin/string.v | 15 +++++++++++---- vlib/builtin/utf8.v | 38 ++++++++++++++++++++++---------------- vlib/v2/token/position.v | 2 +- 4 files changed, 35 insertions(+), 23 deletions(-) diff --git a/vlib/builtin/rune_map.v b/vlib/builtin/rune_map.v index 4f0a297c1b..8c5bcd2563 100644 --- a/vlib/builtin/rune_map.v +++ b/vlib/builtin/rune_map.v @@ -25,8 +25,7 @@ const rune_maps_utl = -2 // NOTE: this should *NOT* be used anywhere in rune_map // It is represented that way, instead of the more natural array of structs, to save on the .c encoding used for the initialisation. // The overhead for representing it as an array of structs was ~28KB in .c, while with the flat array of ints, it is ~7.5KB. // Given that xz can compress it to ~1.8KB, it could be probably represented in an even more compact way... -const rune_maps = [ - i32(0xB5), 0xB5, 743, 0, +const rune_maps = [ i32(0xB5), 0xB5, 743, 0, // this being on the same line, is needed as a workaround for a bug in v2's parser 0xC0, 0xD6, 0, 32, 0xD8, 0xDE, 0, 32, 0xE0, 0xF6, -32, 0, diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index cd5e656102..4709a2de1e 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -2166,10 +2166,13 @@ pub fn (str string) is_hex() bool { } for i < str.len { - if (str[i] < `0` || str[i] > `9`) && ((str[i] < `a` || str[i] > `f`) - && (str[i] < `A` || str[i] > `F`)) { + // TODO: remove this workaround for v2's parser + // vfmt off + if (str[i] < `0` || str[i] > `9`) && + ((str[i] < `a` || str[i] > `f`) && (str[i] < `A` || str[i] > `F`)) { return false } + // vfmt on i++ } @@ -2885,8 +2888,11 @@ pub fn (s string) camel_to_snake() string { c := s[i] c_is_upper := c.is_capital() // Cases: `aBcd == a_bcd` || `ABcd == ab_cd` - if ((c_is_upper && !prev_is_upper) - || (!c_is_upper && prev_is_upper && s[i - 2].is_capital())) && c != `_` { + // TODO: remove this workaround for v2's parser + // vfmt off + if ((c_is_upper && !prev_is_upper) || + (!c_is_upper && prev_is_upper && s[i - 2].is_capital())) && + c != `_` { unsafe { if b[pos - 1] != `_` { b[pos] = `_` @@ -2894,6 +2900,7 @@ pub fn (s string) camel_to_snake() string { } } } + // vfmt on lower_c := if c_is_upper { c + 32 } else { c } unsafe { b[pos] = lower_c diff --git a/vlib/builtin/utf8.v b/vlib/builtin/utf8.v index 84db2f0724..33c1d0f59d 100644 --- a/vlib/builtin/utf8.v +++ b/vlib/builtin/utf8.v @@ -146,10 +146,12 @@ pub fn utf8_str_visible_length(s string) int { // diacritical marks extended // diacritical marks supplement // diacritical marks for symbols - if (r >= 0xe1aab0 && r <= 0xe1ac7f) - || (r >= 0xe1b780 && r <= 0xe1b87f) - || (r >= 0xe28390 && r <= 0xe2847f) - || (r >= 0xefb8a0 && r <= 0xefb8af) { + // TODO: remove this workaround for v2's parser + // vfmt off + if (r >= 0xe1aab0 && r <= 0xe1ac7f) || + (r >= 0xe1b780 && r <= 0xe1b87f) || + (r >= 0xe28390 && r <= 0xe2847f) || + (r >= 0xefb8a0 && r <= 0xefb8af) { // diacritical marks l-- } @@ -157,17 +159,18 @@ pub fn utf8_str_visible_length(s string) int { // CJK Unified Ideographics // Hangru // CJK - else if (r >= 0xe18480 && r <= 0xe1859f) - || (r >= 0xe2ba80 && r <= 0xe2bf95) - || (r >= 0xe38080 && r <= 0xe4b77f) - || (r >= 0xe4b880 && r <= 0xea807f) - || (r >= 0xeaa5a0 && r <= 0xeaa79f) - || (r >= 0xeab080 && r <= 0xed9eaf) - || (r >= 0xefa480 && r <= 0xefac7f) - || (r >= 0xefb8b8 && r <= 0xefb9af) { + else if (r >= 0xe18480 && r <= 0xe1859f) || + (r >= 0xe2ba80 && r <= 0xe2bf95) || + (r >= 0xe38080 && r <= 0xe4b77f) || + (r >= 0xe4b880 && r <= 0xea807f) || + (r >= 0xeaa5a0 && r <= 0xeaa79f) || + (r >= 0xeab080 && r <= 0xed9eaf) || + (r >= 0xefa480 && r <= 0xefac7f) || + (r >= 0xefb8b8 && r <= 0xefb9af) { // half marks l++ } + // vfmt on } 4 { r := u64((u32(c) << 24) | unsafe { @@ -176,12 +179,15 @@ pub fn utf8_str_visible_length(s string) int { // Enclosed Ideographic Supplement // Emoji // CJK Unified Ideographs Extension B-G - if (r >= 0x0f9f8880 && r <= 0xf09f8a8f) - || (r >= 0xf09f8c80 && r <= 0xf09f9c90) - || (r >= 0xf09fa490 && r <= 0xf09fa7af) - || (r >= 0xf0a08080 && r <= 0xf180807f) { + // TODO: remove this workaround for v2's parser + // vfmt off + if (r >= 0x0f9f8880 && r <= 0xf09f8a8f) || + (r >= 0xf09f8c80 && r <= 0xf09f9c90) || + (r >= 0xf09fa490 && r <= 0xf09fa7af) || + (r >= 0xf0a08080 && r <= 0xf180807f) { l++ } + // vfmt on } else {} } diff --git a/vlib/v2/token/position.v b/vlib/v2/token/position.v index f139ce1309..3e517ca28a 100644 --- a/vlib/v2/token/position.v +++ b/vlib/v2/token/position.v @@ -8,7 +8,7 @@ import sync // TODO: finish fileset / file / base pos etc // compact encoding of a source position within a file set -type Pos = int +pub type Pos = int pub struct Position { pub: