Skip to content

Commit b1dbaf0

Browse files
committed
remove some hazards
1 parent 088bb4b commit b1dbaf0

1 file changed

Lines changed: 2 additions & 169 deletions

File tree

vlib/builtin/wasm/array.v

Lines changed: 2 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ fn __at_least_one(how_many u64) u64 {
1313

1414
fn panic_on_negative_len(len int) {
1515
if len < 0 {
16-
panic('negative array length: ${len}')
16+
panic('negative array length')
1717
}
1818
}
1919

2020
@[inline]
2121
fn panic_on_negative_cap(cap int) {
2222
if cap < 0 {
23-
panic('negative array capacity: ${cap}')
23+
panic('negative array capacity')
2424
}
2525
}
2626

@@ -276,170 +276,3 @@ fn (a array) eq(b array) bool {
276276
bytes_to_compare := a.len * a.element_size
277277
return vmemcmp(a.data, b.data, bytes_to_compare) == 0
278278
}
279-
280-
// str returns a string representation of string arrays
281-
// Format: ['elem1', 'elem2', 'elem3']
282-
pub fn (a []string) str() string {
283-
if a.len == 0 {
284-
return '[]'
285-
}
286-
// Build the string manually without using string interpolation or builders
287-
// Calculate total size needed
288-
mut total_len := 2 // []
289-
for i in 0 .. a.len {
290-
s := a[i]
291-
total_len += s.len + 2 // quotes
292-
if i > 0 {
293-
total_len += 2 // ', '
294-
}
295-
}
296-
297-
// Allocate memory for result
298-
mut result_data := unsafe { malloc(total_len + 1) }
299-
mut pos := 0
300-
301-
// Write '['
302-
unsafe {
303-
result_data[pos] = `[`
304-
}
305-
pos++
306-
307-
// Write each element
308-
for i in 0 .. a.len {
309-
// Write comma and space if not first
310-
if i > 0 {
311-
unsafe {
312-
result_data[pos] = `,`
313-
result_data[pos + 1] = ` `
314-
}
315-
pos += 2
316-
}
317-
318-
// Write opening quote
319-
unsafe {
320-
result_data[pos] = `'`
321-
}
322-
pos++
323-
324-
// Write string content
325-
s := a[i]
326-
for j in 0 .. s.len {
327-
unsafe {
328-
result_data[pos] = s.str[j]
329-
}
330-
pos++
331-
}
332-
333-
// Write closing quote
334-
unsafe {
335-
result_data[pos] = `'`
336-
}
337-
pos++
338-
}
339-
340-
// Write ']'
341-
unsafe {
342-
result_data[pos] = `]`
343-
result_data[pos + 1] = 0 // null terminator
344-
}
345-
346-
// Return as string
347-
return string{
348-
str: result_data
349-
len: total_len
350-
}
351-
}
352-
353-
// str returns a string representation of int arrays
354-
// Format: [1, 2, 3]
355-
pub fn (a []int) str() string {
356-
if a.len == 0 {
357-
return '[]'
358-
}
359-
360-
// Build the string manually
361-
// Estimate size: each int can be up to 11 chars (-2147483648), plus commas and spaces
362-
mut total_len := 2 // []
363-
total_len += a.len * 12 // rough estimate per element
364-
total_len += (a.len - 1) * 2 // ', ' separators
365-
366-
// Allocate memory for result
367-
mut result_data := unsafe { malloc(total_len + 1) }
368-
mut pos := 0
369-
370-
// Write '['
371-
unsafe {
372-
result_data[pos] = `[`
373-
}
374-
pos++
375-
376-
// Write each element
377-
for i in 0 .. a.len {
378-
// Write comma and space if not first
379-
if i > 0 {
380-
unsafe {
381-
result_data[pos] = `,`
382-
result_data[pos + 1] = ` `
383-
}
384-
pos += 2
385-
}
386-
387-
// Convert int to string
388-
val := a[i]
389-
mut num_str := ''
390-
if val == 0 {
391-
num_str = '0'
392-
} else {
393-
mut n := val
394-
mut is_negative := false
395-
if n < 0 {
396-
is_negative = true
397-
n = -n
398-
}
399-
400-
// Convert to string
401-
mut digits := []u8{}
402-
for n > 0 {
403-
digits << u8(n % 10 + 48) // ASCII '0' = 48
404-
n /= 10
405-
}
406-
407-
// Build the number string
408-
if is_negative {
409-
unsafe {
410-
result_data[pos] = `-`
411-
}
412-
pos++
413-
}
414-
415-
// Write digits in reverse order
416-
for j := digits.len - 1; j >= 0; j-- {
417-
unsafe {
418-
result_data[pos] = digits[j]
419-
}
420-
pos++
421-
}
422-
continue
423-
}
424-
425-
// Write '0' for zero case
426-
for c in num_str {
427-
unsafe {
428-
result_data[pos] = c
429-
}
430-
pos++
431-
}
432-
}
433-
434-
// Write ']'
435-
unsafe {
436-
result_data[pos] = `]`
437-
result_data[pos + 1] = 0 // null terminator
438-
}
439-
440-
// Return as string
441-
return string{
442-
str: result_data
443-
len: pos
444-
}
445-
}

0 commit comments

Comments
 (0)