Skip to content
This repository was archived by the owner on Feb 14, 2025. It is now read-only.

Commit fff4eea

Browse files
committed
fix broken filewrapper funcs
1 parent 6e07ab5 commit fff4eea

14 files changed

+518
-718
lines changed

gojo/builtins/attributes.mojo

+5-78
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ from collections import InlineList
44
fn copy[
55
T: CollectionElement, is_trivial: Bool
66
](inout target: List[T, is_trivial], source: List[T, is_trivial], start: Int = 0) -> Int:
7-
"""Copies the contents of source into target at the same index. Returns the number of bytes copied.
8-
Added a start parameter to specify the index to start copying into.
7+
"""Copies the contents of source into target at the same index.
98
109
Args:
1110
target: The buffer to copy into.
@@ -27,91 +26,20 @@ fn copy[
2726
return count
2827

2928

30-
# fn copy[T: CollectionElement](inout target_span: Span[T], source_span: Span[T], start: Int = 0) -> Int:
31-
# """Copies the contents of source into target at the same index. Returns the number of bytes copied.
32-
# Added a start parameter to specify the index to start copying into.
33-
34-
# Args:
35-
# target_span: The buffer to copy into.
36-
# source_span: The buffer to copy from.
37-
# start: The index to start copying into.
38-
39-
# Returns:
40-
# The number of bytes copied.
41-
# """
42-
# var count = 0
43-
44-
# for i in range(len(source_span)):
45-
# target_span[i + start] = source_span[i]
46-
# count += 1
47-
48-
# target_span._len += count
49-
# return count
50-
51-
52-
# fn copy[T: CollectionElementNew](inout target_span: Span[T], source: InlineList[T], start: Int = 0) -> Int:
53-
# """Copies the contents of source into target at the same index. Returns the number of bytes copied.
54-
# Added a start parameter to specify the index to start copying into.
55-
56-
# Args:
57-
# target_span: The buffer to copy into.
58-
# source: The buffer to copy from.
59-
# start: The index to start copying into.
60-
61-
# Returns:
62-
# The number of bytes copied.
63-
# """
64-
# var count = 0
65-
66-
# for i in range(len(source)):
67-
# target_span[i + start] = source[i]
68-
# count += 1
69-
70-
# target_span._len += count
71-
# return count
72-
73-
74-
# fn copy[T: CollectionElementNew, T2: CollectionElement](inout list: InlineList[T], source: Span[T2], start: Int = 0) -> Int:
75-
# """Copies the contents of source into target at the same index. Returns the number of bytes copied.
76-
# Added a start parameter to specify the index to start copying into.
77-
78-
# Args:
79-
# list: The buffer to copy into.
80-
# source: The buffer to copy from.
81-
# start: The index to start copying into.
82-
83-
# Returns:
84-
# The number of bytes copied.
85-
# """
86-
# var count = 0
87-
88-
# for i in range(len(source)):
89-
# if i + start > len(list):
90-
# list[i + start] = source[i]
91-
# else:
92-
# list.append(source[i])
93-
# count += 1
94-
95-
# return count
96-
97-
98-
fn copy(target: UnsafePointer[UInt8], source: UnsafePointer[UInt8], source_length: Int, start: Int = 0) -> Int:
99-
"""Copies the contents of source into target at the same index. Returns the number of bytes copied.
100-
Added a start parameter to specify the index to start copying into.
29+
fn copy(target: UnsafePointer[UInt8], source: UnsafePointer[UInt8], source_length: Int) -> Int:
30+
"""Copies the contents of source into target at the same index.
10131
10232
Args:
10333
target: The buffer to copy into.
10434
source: The buffer to copy from.
10535
source_length: The length of the source buffer.
106-
start: The index to start copying into.
10736
10837
Returns:
10938
The number of bytes copied.
11039
"""
11140
var count = 0
112-
11341
for i in range(source_length):
114-
target[i + start] = source[i]
42+
target[i] = source[i]
11543
count += 1
11644

11745
return count
@@ -124,8 +52,7 @@ fn copy(
12452
source_end: Int,
12553
target_start: Int = 0,
12654
) -> Int:
127-
"""Copies the contents of source into target at the same index. Returns the number of bytes copied.
128-
Added a start parameter to specify the index to start copying into.
55+
"""Copies the contents of source into target at the same index.
12956
13057
Args:
13158
target: The buffer to copy into.

gojo/builtins/bytes.mojo

+24-23
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ alias Byte = UInt8
44

55

66
fn equals[is_trivial: Bool](left: List[UInt8, is_trivial], right: List[UInt8, is_trivial]) -> Bool:
7+
"""Reports if `left` and `right` are equal.
8+
9+
Args:
10+
left: The first list to compare.
11+
right: The second list to compare.
12+
"""
713
if len(left) != len(right):
814
return False
915
for i in range(len(left)):
@@ -13,44 +19,38 @@ fn equals[is_trivial: Bool](left: List[UInt8, is_trivial], right: List[UInt8, is
1319

1420

1521
fn has_prefix[is_trivial: Bool](bytes: List[Byte, is_trivial], prefix: List[Byte, is_trivial]) -> Bool:
16-
"""Reports whether the List[Byte] struct begins with prefix.
22+
"""Reports if the list begins with prefix.
1723
1824
Args:
19-
bytes: The List[Byte] struct to search.
25+
bytes: The list to search.
2026
prefix: The prefix to search for.
21-
22-
Returns:
23-
True if the List[Byte] struct begins with prefix; otherwise, False.
2427
"""
2528
var len_comparison = len(bytes) >= len(prefix)
2629
var prefix_comparison = equals(bytes[0 : len(prefix)], prefix)
2730
return len_comparison and prefix_comparison
2831

2932

3033
fn has_suffix[is_trivial: Bool](bytes: List[Byte, is_trivial], suffix: List[Byte, is_trivial]) -> Bool:
31-
"""Reports whether the List[Byte] struct ends with suffix.
34+
"""Reports if the list ends with suffix.
3235
3336
Args:
34-
bytes: The List[Byte] struct to search.
35-
suffix: The prefix to search for.
36-
37-
Returns:
38-
True if the List[Byte] struct ends with suffix; otherwise, False.
37+
bytes: The list struct to search.
38+
suffix: The suffix to search for.
3939
"""
4040
var len_comparison = len(bytes) >= len(suffix)
4141
var suffix_comparison = equals(bytes[len(bytes) - len(suffix) : len(bytes)], suffix)
4242
return len_comparison and suffix_comparison
4343

4444

4545
fn index_byte[is_trivial: Bool](bytes: List[Byte, is_trivial], delim: Byte) -> Int:
46-
"""Return the index of the first occurrence of the byte delim.
46+
"""Return the index of the first occurrence of the byte `delim`.
4747
4848
Args:
49-
bytes: The List[Byte] struct to search.
49+
bytes: The list to search.
5050
delim: The byte to search for.
5151
5252
Returns:
53-
The index of the first occurrence of the byte delim.
53+
The index of the first occurrence of the byte `delim`.
5454
"""
5555
for i in range(len(bytes)):
5656
if bytes[i] == delim:
@@ -60,15 +60,15 @@ fn index_byte[is_trivial: Bool](bytes: List[Byte, is_trivial], delim: Byte) -> I
6060

6161

6262
fn index_byte(bytes: UnsafePointer[Scalar[DType.uint8]], size: Int, delim: Byte) -> Int:
63-
"""Return the index of the first occurrence of the byte delim.
63+
"""Return the index of the first occurrence of the byte `delim`.
6464
6565
Args:
66-
bytes: The DTypePointer[DType.int8] struct to search.
67-
size: The size of the bytes pointer.
66+
bytes: The list to search.
67+
size: The number of elements stored at the pointer address.
6868
delim: The byte to search for.
6969
7070
Returns:
71-
The index of the first occurrence of the byte delim.
71+
The index of the first occurrence of the byte `delim`.
7272
"""
7373
for i in range(size):
7474
if UInt8(bytes[i]) == delim:
@@ -78,14 +78,14 @@ fn index_byte(bytes: UnsafePointer[Scalar[DType.uint8]], size: Int, delim: Byte)
7878

7979

8080
fn index_byte(bytes: Span[UInt8], delim: Byte) -> Int:
81-
"""Return the index of the first occurrence of the byte delim.
81+
"""Return the index of the first occurrence of the byte `delim`.
8282
8383
Args:
8484
bytes: The Span to search.
8585
delim: The byte to search for.
8686
8787
Returns:
88-
The index of the first occurrence of the byte delim.
88+
The index of the first occurrence of the byte `delim`.
8989
"""
9090
for i in range(len(bytes)):
9191
if bytes[i] == delim:
@@ -95,13 +95,14 @@ fn index_byte(bytes: Span[UInt8], delim: Byte) -> Int:
9595

9696

9797
fn to_string[is_trivial: Bool](bytes: List[Byte, is_trivial]) -> String:
98-
"""Makes a deepcopy of the List[Byte] supplied and converts it to a string. If it's not null terminated, it will append a null byte.
98+
"""Makes a deep copy of the list supplied and converts it to a string.
99+
If it's not null terminated, it will append a null byte.
99100
100101
Args:
101-
bytes: The List[Byte] struct to convert.
102+
bytes: The list to convert.
102103
103104
Returns:
104-
The string representation of the List[Byte] struct.
105+
A String built from the list of bytes.
105106
"""
106107
var copy = List[Byte](bytes)
107108
if copy[-1] != 0:

gojo/builtins/errors.mojo

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from sys import exit
22

33

4-
fn panic[T: Stringable](message: T, code: Int = 1):
4+
fn panic[T: Stringable](message: T, code: Int = 1) -> None:
55
"""Panics the program with the given message and exit code.
66
77
Args:

gojo/bytes/buffer.mojo

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ struct Buffer(
194194
"""
195195
return self.as_string_slice()
196196

197-
@deprecated("Buffer.render() has been deprecated. Use Buffer.as_string_slice() instead.")
197+
@deprecated("Buffer.render() has been deprecated. Use Buffer.as_string_slice() or call str() instead.")
198198
fn render(self) -> StringSlice[__lifetime_of(self)]:
199199
"""
200200
Return a StringSlice view of the data owned by the builder.

gojo/fmt/fmt.mojo

+72-3
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,15 @@ fn find_first_verb(s: String, verbs: List[String]) -> String:
7575

7676

7777
fn format_string(format: String, arg: String) -> String:
78+
"""Format a string argument.
79+
80+
Args:
81+
format: The format string.
82+
arg: The string argument.
83+
84+
Returns:
85+
The formatted string.
86+
"""
7887
var verb = find_first_verb(format, List[String]("%s", "%q"))
7988
var arg_to_place = arg
8089
if verb == "%q":
@@ -84,6 +93,15 @@ fn format_string(format: String, arg: String) -> String:
8493

8594

8695
fn format_bytes(format: String, arg: List[UInt8, True]) -> String:
96+
"""Format a byte list argument.
97+
98+
Args:
99+
format: The format string.
100+
arg: The byte list argument.
101+
102+
Returns:
103+
The formatted byte list.
104+
"""
87105
var argument = arg
88106
if argument[-1] != 0:
89107
argument.append(0)
@@ -92,6 +110,15 @@ fn format_bytes(format: String, arg: List[UInt8, True]) -> String:
92110

93111

94112
fn format_integer(format: String, arg: Int) -> String:
113+
"""Format an integer argument.
114+
115+
Args:
116+
format: The format string.
117+
arg: The integer argument.
118+
119+
Returns:
120+
The formatted integer.
121+
"""
95122
var verb = find_first_verb(format, List[String]("%d", "%q"))
96123
var arg_to_place = str(arg)
97124
if verb == "%q":
@@ -101,28 +128,55 @@ fn format_integer(format: String, arg: Int) -> String:
101128

102129

103130
fn format_float(format: String, arg: Float64) -> String:
131+
"""Format a float argument.
132+
133+
Args:
134+
format: The format string.
135+
arg: The float argument.
136+
137+
Returns:
138+
The formatted float.
139+
"""
104140
return replace_first(format, str("%f"), str(arg))
105141

106142

107143
fn format_boolean(format: String, arg: Bool) -> String:
144+
"""Format a boolean argument.
145+
146+
Args:
147+
format: The format string.
148+
arg: The boolean argument.
149+
150+
Returns:
151+
The formatted boolean.
152+
"""
108153
var value: String = "False"
109154
if arg:
110155
value = "True"
111156

112157
return replace_first(format, String("%t"), value)
113158

114159

115-
# If the number of arguments does not match the number of format specifiers
116-
alias BadArgCount = "(BAD ARG COUNT)"
160+
alias BAD_ARG_COUNT = "(BAD ARG COUNT)"
161+
"""If the number of arguments does not match the number of format specifiers."""
117162

118163

119164
fn sprintf(formatting: String, *args: Args) -> String:
165+
"""Format a string with the given arguments.
166+
167+
Args:
168+
formatting: The format string.
169+
args: The arguments to format the string with.
170+
171+
Returns:
172+
The formatted string.
173+
"""
120174
var text = formatting
121175
var raw_percent_count = formatting.count("%%") * 2
122176
var formatter_count = formatting.count("%") - raw_percent_count
123177

124178
if formatter_count != len(args):
125-
return BadArgCount
179+
return BAD_ARG_COUNT
126180

127181
for i in range(len(args)):
128182
var argument = args[i]
@@ -142,6 +196,15 @@ fn sprintf(formatting: String, *args: Args) -> String:
142196

143197
# TODO: temporary until we have arg packing.
144198
fn sprintf_str(formatting: String, args: List[String]) raises -> String:
199+
"""Format a string with the given arguments.
200+
201+
Args:
202+
formatting: The format string.
203+
args: The arguments to format the string with.
204+
205+
Returns:
206+
The formatted string.
207+
"""
145208
var text = formatting
146209
var formatter_count = formatting.count("%")
147210

@@ -157,6 +220,12 @@ fn sprintf_str(formatting: String, args: List[String]) raises -> String:
157220

158221

159222
fn printf(formatting: String, *args: Args) raises:
223+
"""Print a formatted string with the given arguments.
224+
225+
Args:
226+
formatting: The format string.
227+
args: The arguments to format the string with.
228+
"""
160229
var text = formatting
161230
var raw_percent_count = formatting.count("%%") * 2
162231
var formatter_count = formatting.count("%") - raw_percent_count

0 commit comments

Comments
 (0)