Skip to content

Commit

Permalink
Merge branch 'nightly' into ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
jjvraw authored Oct 17, 2024
2 parents 33b7363 + f840ba2 commit 6f839a6
Show file tree
Hide file tree
Showing 13 changed files with 846 additions and 430 deletions.
2 changes: 1 addition & 1 deletion stdlib/COMPATIBLE_COMPILER_VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2024.10.1619
2024.10.1705
6 changes: 4 additions & 2 deletions stdlib/benchmarks/math/bench_math.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ fn bench_math[
@parameter
fn call_fn() raises:
for input in inputs:
_ = math_f1p(input[])
var result = math_f1p(input[])
keep(result)

b.iter[call_fn]()

Expand All @@ -70,7 +71,8 @@ fn bench_math3[
@parameter
fn call_fn() raises:
for input in inputs:
_ = math_f3p(input[], input[], input[])
var result = math_f3p(input[], input[], input[])
keep(result)

b.iter[call_fn]()

Expand Down
4 changes: 2 additions & 2 deletions stdlib/src/builtin/_math.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ trait Ceilable:
var im: Float64
fn __ceil__(self) -> Self:
return Self(ceil(re), ceil(im))
return Self(ceil(self.re), ceil(self.im))
```
"""

Expand Down Expand Up @@ -78,7 +78,7 @@ trait Floorable:
var im: Float64
fn __floor__(self) -> Self:
return Self(floor(re), floor(im))
return Self(floor(self.re), floor(self.im))
```
"""

Expand Down
4 changes: 3 additions & 1 deletion stdlib/src/builtin/floatable.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ trait FloatableRaising:
For example:
```mojo
from utils import Variant
@value
struct MaybeFloat(FloatableRasing):
struct MaybeFloat(FloatableRaising):
var value: Variant[Float64, NoneType]
fn __float__(self) raises -> Float64:
Expand Down
48 changes: 38 additions & 10 deletions stdlib/src/builtin/string_literal.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ from utils import StringRef, Span, StringSlice, StaticString
from utils import Formattable, Formatter
from utils._visualizers import lldb_formatter_wrapping_type

from collections.string import _atol, _StringSliceIter
from utils.string_slice import (
_StringSliceIter,
_FormatCurlyEntry,
_CurlyEntryFormattable,
)

# ===----------------------------------------------------------------------===#
# StringLiteral
Expand Down Expand Up @@ -206,27 +210,25 @@ struct StringLiteral(
"""
return len(self) != 0

@always_inline
fn __int__(self) raises -> Int:
"""Parses the given string as a base-10 integer and returns that value.
For example, `int("19")` returns `19`. If the given string cannot be parsed
as an integer value, an error is raised. For example, `int("hi")` raises an
error.
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)
return int(self.as_string_slice())

@always_inline
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.
"""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)
return float(self.as_string_slice())

@no_inline
fn __str__(self) -> String:
Expand Down Expand Up @@ -414,6 +416,32 @@ struct StringLiteral(
len=self.byte_length(),
)

@always_inline
fn format[*Ts: _CurlyEntryFormattable](self, *args: *Ts) raises -> String:
"""Format a template with `*args`.
Args:
args: The substitution values.
Parameters:
Ts: The types of substitution values that implement `Representable`
and `Stringable` (to be changed and made more flexible).
Returns:
The template with the given values substituted.
Examples:
```mojo
# Manual indexing:
print("{0} {1} {0}".format("Mojo", 1.125)) # Mojo 1.125 Mojo
# Automatic indexing:
print("{} {}".format(True, "hello world")) # True hello world
```
.
"""
return _FormatCurlyEntry.format(self, args)

fn format_to(self, inout writer: Formatter):
"""
Formats this string literal to the provided formatter.
Expand Down
Loading

0 comments on commit 6f839a6

Please sign in to comment.