Skip to content

Commit

Permalink
more perf improvements and add fixme for PR modular#3577
Browse files Browse the repository at this point in the history
Signed-off-by: martinvuyk <martin.vuyklop@gmail.com>
  • Loading branch information
martinvuyk committed Sep 30, 2024
1 parent 9e5fbbd commit a92c24e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 18 deletions.
37 changes: 20 additions & 17 deletions stdlib/src/collections/string.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -2084,8 +2084,12 @@ struct String(
debug_assert(
pos_in_self < s_len, "pos_in_self >= self.byte_length()"
)
res += String(
_build_slice(self.unsafe_ptr(), pos_in_self, e[].first_curly)
res += (
String( # FIXME(#3577): remove String constructor once it lands
_build_slice(
self.unsafe_ptr(), pos_in_self, e[].first_curly
)
)
)

if e[].is_escaped_brace():
Expand Down Expand Up @@ -2116,7 +2120,9 @@ struct String(
pos_in_self = e[].last_curly + 1

if pos_in_self < s_len:
res += String(_build_slice(self.unsafe_ptr(), pos_in_self, s_len))
res += String(
_build_slice(self.unsafe_ptr(), pos_in_self, s_len)
) # FIXME(#3577): remove String constructor once it lands

return res^

Expand Down Expand Up @@ -2587,9 +2593,8 @@ struct _FormatCurlyEntry(CollectionElement, CollectionElementNew):
unsafe_from_utf8_ptr=s_ptr + start, len=end - start
)

var field = String(
_build_slice(format_src.unsafe_ptr(), start_value + 1, i)
)
var field = _build_slice(format_src.unsafe_ptr(), start_value + 1, i)
var field_b_len = i - (start_value + 1)
# FIXME(#3526): this will break once find works with unicode codepoints
var exclamation_index = field.find("!")

Expand All @@ -2602,26 +2607,23 @@ struct _FormatCurlyEntry(CollectionElement, CollectionElementNew):
# 3. adjusting the field and conversion_flag parsing accordingly

if exclamation_index != -1:
field_b_len = i - (start_value + 1)
var new_idx = exclamation_index + 1
if new_idx < field_b_len:
var conversion_flag = field._buffer.unsafe_get(new_idx)
var f_ptr = field.unsafe_ptr()
var conversion_flag = f_ptr[new_idx]
if field_b_len - new_idx > 1 or (
conversion_flag not in supported_conversion_flags
):
var f = String(
_build_slice(field.unsafe_ptr(), new_idx, field_b_len)
)
_ = field
var f = String(_build_slice(f_ptr, new_idx, field_b_len))
_ = field^
raise Error('Conversion flag "' + f + '" not recognised.')
self.conversion_flag = conversion_flag
else:
raise "Empty conversion flag."

field._buffer.resize(new_idx)
field._buffer.unsafe_set(exclamation_index, 0)
field = _build_slice(field.unsafe_ptr(), 0, exclamation_index)

if field._buffer.unsafe_get(0) == 0:
if field.byte_length() == 0:
# an empty field, so it's automatic indexing
if automatic_indexing_count >= len_pos_args:
raised_automatic_index = automatic_indexing_count
Expand All @@ -2642,7 +2644,8 @@ struct _FormatCurlyEntry(CollectionElement, CollectionElementNew):
"Not the expected error from atol",
)
# field is a keyword for **kwargs:
self.field = field
raised_kwarg_field = field
var f = str(field)
self.field = f
raised_kwarg_field = f
return True
return False
20 changes: 19 additions & 1 deletion stdlib/src/utils/string_slice.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ from utils import StringSlice

from bit import count_leading_zeros
from utils import Span
from collections.string import _isspace
from collections.string import _isspace, _atol, _atof
from collections import List
from memory import memcmp, UnsafePointer
from sys import simdwidthof, bitwidthof
Expand Down Expand Up @@ -454,6 +454,24 @@ struct StringSlice[
unsafe_pointer=self.unsafe_ptr(), length=self.byte_length()
)

fn __int__(self) raises -> Int:
"""Parses the given string as a base-10 integer and returns that value.
If the string cannot be parsed as an int, an error is raised.
Returns:
An integer value that represents the string, or otherwise raises.
"""
return _atol(self._strref_dangerous())

fn __float__(self) raises -> Float64:
"""Parses the string as a float point number and returns that value. If
the string cannot be parsed as a float, an error is raised.
Returns:
A float value that represents the string, or otherwise raises.
"""
return _atof(self._strref_dangerous())

# ===------------------------------------------------------------------===#
# Methods
# ===------------------------------------------------------------------===#
Expand Down

0 comments on commit a92c24e

Please sign in to comment.