Skip to content

Commit 20763ba

Browse files
committed
nice
1 parent 4e9abd4 commit 20763ba

12 files changed

Lines changed: 140 additions & 48 deletions

File tree

vlib/builtin/rune.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub fn (c rune) repeat(count int) string {
3737
}
3838

3939
// bytes converts a rune to an array of bytes.
40-
@[manualfree]
40+
@[manualfree; markused]
4141
pub fn (c rune) bytes() []u8 {
4242
mut res := []u8{cap: 5}
4343
mut buf := &u8(res.data)

vlib/strconv/atof.c.v

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,13 +429,14 @@ fn converter(mut pn PrepNumber) u64 {
429429
return result
430430
}
431431

432-
@[params]
432+
@[markused; params]
433433
pub struct AtoF64Param {
434434
pub:
435435
allow_extra_chars bool // allow extra characters after number
436436
}
437437

438438
// atof64 parses the string `s`, and if possible, converts it into a f64 number
439+
@[markused]
439440
pub fn atof64(s string, param AtoF64Param) !f64 {
440441
if s.len == 0 {
441442
return error('expected a number found an empty string')

vlib/strconv/atoi.v

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ pub fn common_parse_uint2(s string, _base int, _bit_size int) (u64, int) {
147147
}
148148

149149
// parse_uint is like parse_int but for unsigned numbers.
150+
@[markused]
150151
pub fn parse_uint(s string, _base int, _bit_size int) !u64 {
151152
return common_parse_uint(s, _base, _bit_size, true, true)
152153
}
@@ -286,31 +287,35 @@ fn atoi_common(s string, type_min i64, type_max i64) !i64 {
286287

287288
// atoi is equivalent to parse_int(s, 10, 0), converted to type int.
288289
// It follows V scanner as much as observed.
290+
@[markused]
289291
pub fn atoi(s string) !int {
290292
return int(atoi_common(s, i64_min_int32, i64_max_int32)!)
291293
}
292294

293295
// atoi8 is equivalent to atoi(s), converted to type i8.
294296
// returns an i8 [-128 .. 127] or an error.
297+
@[markused]
295298
pub fn atoi8(s string) !i8 {
296299
return i8(atoi_common(s, min_i8, max_i8)!)
297300
}
298301

299302
// atoi16 is equivalent to atoi(s), converted to type i16.
300303
// returns an i16 [-32678 .. 32767] or an error.
304+
@[markused]
301305
pub fn atoi16(s string) !i16 {
302306
return i16(atoi_common(s, min_i16, max_i16)!)
303307
}
304308

305309
// atoi32 is equivalent to atoi(s), converted to type i32.
306310
// returns an i32 [-2147483648 .. 2147483647] or an error.
311+
@[markused]
307312
pub fn atoi32(s string) !i32 {
308313
return i32(atoi_common(s, min_i32, max_i32)!)
309314
}
310315

311316
// atoi64 converts radix 10 string to i64 type.
312317
// returns an i64 [-9223372036854775808 .. 9223372036854775807] or an error.
313-
@[direct_array_access]
318+
@[direct_array_access; markused]
314319
pub fn atoi64(s string) !i64 {
315320
mut sign, mut start_idx := atoi_common_check(s)!
316321
mut x := i64(0)

vlib/strconv/atou.v

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,29 +67,34 @@ fn atou_common(s string, type_max u64) !u64 {
6767

6868
// atou8 is equivalent to parse_uint(s, 10, 0), converted to type u8.
6969
// It returns u8 in range [0..255] or an Error.
70+
@[markused]
7071
pub fn atou8(s string) !u8 {
7172
return u8(atou_common(s, max_u8)!)
7273
}
7374

7475
// atou16 is equivalent to parse_uint(s, 10, 0), converted to type u16.
7576
// It returns u16 in range [0..65535] or an Error.
77+
@[markused]
7678
pub fn atou16(s string) !u16 {
7779
return u16(atou_common(s, max_u16)!)
7880
}
7981

8082
// atou is equivalent to parse_uint(s, 10, 0), converted to type u32.
83+
@[markused]
8184
pub fn atou(s string) !u32 {
8285
return u32(atou_common(s, max_u32)!)
8386
}
8487

8588
// atou32 is identical to atou. Here to provide a symmetrical API with atoi/atoi32
8689
// It returns u32 in range [0..4294967295] or an Error.
90+
@[markused]
8791
pub fn atou32(s string) !u32 {
8892
return u32(atou_common(s, max_u32)!)
8993
}
9094

9195
// atou64 is equivalent to parse_uint(s, 10, 0), converted to type u64.
9296
// It returns u64 in range [0..18446744073709551615] or an Error.
97+
@[markused]
9398
pub fn atou64(s string) !u64 {
9499
return u64(atou_common(s, max_u64)!)
95100
}

vlib/time/time.v

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub const days_before = [
3535
]!
3636

3737
// Time contains various time units for a point in time.
38+
@[markused]
3839
pub struct Time {
3940
unix i64
4041
pub:

vlib/v/markused/walker.v

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,18 @@ mut:
3636
all_decltypes map[string]ast.TypeDecl
3737
all_structs map[string]ast.StructDecl
3838

39-
cur_fn string
40-
cur_fn_concrete_types []ast.Type
41-
level int
42-
is_builtin_mod bool
43-
is_direct_array_access bool
44-
inside_in_op bool
45-
inside_comptime int
46-
inside_comptime_if int
47-
used_fn_generic_types map[string][][]ast.Type
48-
walked_fn_generic_types map[string][][]ast.Type
49-
keep_all_fn_generic_types map[string]bool
39+
cur_fn string
40+
cur_fn_concrete_types []ast.Type
41+
level int
42+
is_builtin_mod bool
43+
is_direct_array_access bool
44+
inside_in_op bool
45+
inside_comptime int
46+
inside_comptime_if int
47+
used_fn_generic_types map[string][][]ast.Type
48+
walked_fn_generic_types map[string][][]ast.Type
49+
keep_all_fn_generic_types map[string]bool
50+
json2_encode_field_helpers map[string]bool
5051

5152
// dependencies finding flags
5253
uses_atomic bool // has atomic
@@ -269,6 +270,11 @@ fn (mut w Walker) mark_json2_optional_field_helpers(concrete_typ ast.Type) {
269270
}
270271

271272
fn (mut w Walker) mark_json2_encode_field_helpers(receiver_typ ast.Type, concrete_typ ast.Type) {
273+
helper_key := '${int(receiver_typ)}:${int(concrete_typ)}'
274+
if w.json2_encode_field_helpers[helper_key] {
275+
return
276+
}
277+
w.json2_encode_field_helpers[helper_key] = true
272278
concrete_sym := w.table.final_sym(w.table.unaliased_type(concrete_typ))
273279
if concrete_sym.kind != .struct || concrete_sym.info !is ast.Struct {
274280
return
@@ -1483,6 +1489,10 @@ fn (mut w Walker) fn_decl_with_concrete_types(mut node ast.FnDecl, concrete_type
14831489
return
14841490
}
14851491
w.mark_fn_as_used(fkey)
1492+
if node.mod == 'x.json2' {
1493+
w.mark_by_sym_name('EnumData')
1494+
w.mark_by_sym_name('time.Time')
1495+
}
14861496
last_is_direct_array_access := w.is_direct_array_access
14871497
w.is_direct_array_access = node.is_direct_arr || w.pref.no_bounds_checking
14881498
defer { w.is_direct_array_access = last_is_direct_array_access }
@@ -1576,7 +1586,8 @@ fn (mut w Walker) fn_decl_with_concrete_types(mut node ast.FnDecl, concrete_type
15761586
continue
15771587
}
15781588
concrete_typ := w.table.unaliased_type(concrete_type_list[0])
1579-
if w.table.final_sym(concrete_typ).kind == .struct {
1589+
concrete_sym := w.table.final_sym(concrete_typ)
1590+
if concrete_sym.kind == .struct && concrete_sym.name != 'time.Time' {
15801591
w.fn_decl_with_concrete_types(mut check_struct_type_valid_fn,
15811592
concrete_type_list)
15821593
}
@@ -1597,7 +1608,7 @@ fn (mut w Walker) fn_decl_with_concrete_types(mut node ast.FnDecl, concrete_type
15971608
}
15981609
}
15991610
}
1600-
if concrete_sym.kind == .struct {
1611+
if concrete_sym.kind == .struct && concrete_sym.name != 'time.Time' {
16011612
if mut decode_struct_key_fn := w.all_fns['x.json2.decode_struct_key'] {
16021613
w.fn_decl_with_concrete_types(mut decode_struct_key_fn, resolved_concrete_types)
16031614
}
@@ -1611,6 +1622,10 @@ fn (mut w Walker) fn_decl_with_concrete_types(mut node ast.FnDecl, concrete_type
16111622
if node.mod == 'x.json2' && node.name == 'decode_struct_key' && resolved_concrete_types.len == 1 {
16121623
w.mark_json2_optional_field_helpers(resolved_concrete_types[0])
16131624
}
1625+
if node.mod == 'x.json2' && node.name == 'decode_enum' {
1626+
w.uses_ct_values = true
1627+
w.mark_by_sym_name('EnumData')
1628+
}
16141629
if node.mod == 'x.json2' && node.name == 'encode_value' && node.is_method
16151630
&& resolved_concrete_types.len == 1 {
16161631
concrete_typ := w.table.unaliased_type(resolved_concrete_types[0])
@@ -1622,7 +1637,7 @@ fn (mut w Walker) fn_decl_with_concrete_types(mut node ast.FnDecl, concrete_type
16221637
}
16231638
}
16241639
}
1625-
if concrete_sym.kind in [.array, .array_fixed] {
1640+
if concrete_sym.kind == .array {
16261641
encode_array_fkey, _ := w.resolve_method_fkey_for_type(node.receiver.typ,
16271642
'encode_array')
16281643
if encode_array_fkey != '' {
@@ -2030,7 +2045,8 @@ pub fn (mut w Walker) call_expr(mut node ast.CallExpr) {
20302045
w.mark_by_type(concrete_type)
20312046
}
20322047
generic_call_inside_generic_caller := w.fn_generic_names(stmt).len > 0
2033-
&& node.raw_concrete_types.len == 0 && caller_generic_names.len > 0
2048+
&& call_concrete_types.len == 0 && node.raw_concrete_types.len == 0
2049+
&& caller_generic_names.len > 0
20342050
keep_all_generic_types := (stmt.generic_names.len > 0 && call_concrete_types.len == 0)
20352051
|| generic_call_inside_generic_caller
20362052
if keep_all_generic_types {

vlib/x/json2/attr_utils.v

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ fn json_attr_value(attr string) ?string {
2020
return unquote_attr_value(attr[5..])
2121
}
2222

23+
@[markused]
2324
fn json_attr_value_range(attr string) ?(int, int) {
2425
if !attr.starts_with('json:') {
2526
return none

vlib/x/json2/decode.v

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module json2
22

33
import strconv
4+
import time
45

56
const null_in_string = 'null'
67

@@ -36,6 +37,7 @@ struct DecoderFieldInfo {
3637
is_raw bool
3738
}
3839

40+
@[markused]
3941
struct StructFieldInfo {
4042
json_name_ptr voidptr
4143
json_name_len int
@@ -375,15 +377,15 @@ fn (mut decoder Decoder) cached_struct_field_infos[T]() []StructFieldInfo {
375377
return *field_infos
376378
}
377379

378-
@[inline]
380+
@[inline; markused]
379381
fn struct_field_is_decoded(decoded_mask u64, decoded_fields []bool, field_idx int) bool {
380382
if field_idx < 64 {
381383
return (decoded_mask & (u64(1) << u64(field_idx))) != 0
382384
}
383385
return decoded_fields[field_idx]
384386
}
385387

386-
@[inline]
388+
@[inline; markused]
387389
fn mark_struct_field_decoded(decoded_mask u64, mut decoded_fields []bool, field_idx int) u64 {
388390
if field_idx < 64 {
389391
return decoded_mask | (u64(1) << u64(field_idx))
@@ -402,7 +404,7 @@ fn (decoder &Decoder) json_key_matches(key_info ValueInfo, key_name string) bool
402404
}
403405
}
404406

405-
@[inline]
407+
@[inline; markused]
406408
fn (decoder &Decoder) is_empty_value(value_info ValueInfo) bool {
407409
match value_info.value_kind {
408410
.null {
@@ -430,6 +432,7 @@ fn (decoder &Decoder) is_empty_value(value_info ValueInfo) bool {
430432
return false
431433
}
432434

435+
@[markused]
433436
fn (mut decoder Decoder) skip_current_value() {
434437
if decoder.current_node == unsafe { nil } {
435438
return
@@ -673,6 +676,23 @@ fn (mut decoder Decoder) decode_value[T](mut val T) ! {
673676
return
674677
} $else $if T.unaliased_typ is string {
675678
decoder.decode_string(mut val)!
679+
} $else $if T.unaliased_typ is time.Time {
680+
value_info := decoder.current_node.value
681+
mut decoded_time := time.Time{}
682+
if value_info.value_kind == .string {
683+
decoded_time.from_json_string(decoder.json[value_info.position + 1..
684+
value_info.position + value_info.length - 1]) or {
685+
decoder.decode_error('${typeof(val).name}: ${err.msg()}')!
686+
}
687+
} else if value_info.value_kind == .number {
688+
decoded_time.from_json_number(decoder.json[value_info.position..value_info.position +
689+
value_info.length]) or {
690+
decoder.decode_error('${typeof(val).name}: ${err.msg()}')!
691+
}
692+
} else {
693+
decoder.decode_error('Expected string or number, but got ${value_info.value_kind}')!
694+
}
695+
val = T(decoded_time)
676696
} $else $if T.unaliased_typ is $sumtype {
677697
decoder.decode_sumtype(mut val)!
678698
return
@@ -1173,6 +1193,7 @@ fn (mut decoder Decoder) decode_enum[T](mut val T) ! {
11731193

11741194
const max_integer_number_digits = 20
11751195

1196+
@[markused]
11761197
fn has_exponent_number_syntax(str string) bool {
11771198
for c in str {
11781199
if c == `e` || c == `E` {
@@ -1182,6 +1203,7 @@ fn has_exponent_number_syntax(str string) bool {
11821203
return false
11831204
}
11841205

1206+
@[markused]
11851207
fn scientific_number_to_integer_string(str string) !string {
11861208
if !has_exponent_number_syntax(str) {
11871209
// Handle plain decimal numbers with zero fractional part (e.g., "-123.0")
@@ -1326,8 +1348,10 @@ fn parse_integer_number[T](str string) !T {
13261348
return T(strconv.atou32(int_str)!)
13271349
} $else $if T.unaliased_typ is u64 {
13281350
return T(strconv.atou64(int_str)!)
1351+
} $else $if T is int {
1352+
return int(strconv.atoi64(int_str)!)
13291353
} $else $if T.unaliased_typ is int {
1330-
return T(strconv.atoi(int_str)!)
1354+
return T(int(strconv.atoi64(int_str)!))
13311355
} $else $if T.unaliased_typ is isize {
13321356
return T(isize(strconv.atoi64(int_str)!))
13331357
} $else $if T.unaliased_typ is usize {
@@ -1337,6 +1361,12 @@ fn parse_integer_number[T](str string) !T {
13371361
}
13381362
}
13391363

1364+
@[markused]
1365+
fn parse_int_number(str string) !int {
1366+
int_str := scientific_number_to_integer_string(str)!
1367+
return int(strconv.atoi64(int_str)!)
1368+
}
1369+
13401370
fn parse_float_number[T](str string) !T {
13411371
$if js {
13421372
$if T.unaliased_typ is f32 {
@@ -1372,7 +1402,7 @@ fn (mut decoder Decoder) decode_number[T](val &T) ! {
13721402
u16 { *val = parse_integer_number[T](str)! }
13731403
u32 { *val = parse_integer_number[T](str)! }
13741404
u64 { *val = parse_integer_number[T](str)! }
1375-
int { *val = parse_integer_number[T](str)! }
1405+
int { *val = parse_int_number(str)! }
13761406
isize { *val = parse_integer_number[T](str)! }
13771407
usize { *val = parse_integer_number[T](str)! }
13781408
f32 { *val = parse_float_number[T](str)! }
@@ -1406,6 +1436,8 @@ fn (mut decoder Decoder) decode_number_from_string[T]() !T {
14061436
return parse_integer_number[T](str)!
14071437
} $else $if T.unaliased_typ is u64 {
14081438
return parse_integer_number[T](str)!
1439+
} $else $if T is int {
1440+
return parse_int_number(str)!
14091441
} $else $if T.unaliased_typ is int {
14101442
return parse_integer_number[T](str)!
14111443
} $else $if T.unaliased_typ is isize {

0 commit comments

Comments
 (0)