mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 03:51:18 +00:00
- Remove dependency on temp allocator in String.join.
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
module std::core::string;
|
module std::core::string;
|
||||||
import std::io, std::ascii;
|
import std::io, std::ascii;
|
||||||
|
import std::core::mem::allocator;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
typedef String @if(!$defined(String)) = inline char[];
|
typedef String @if(!$defined(String)) = inline char[];
|
||||||
@@ -96,7 +98,8 @@ fn ZString tformat_zstr(String fmt, args...) @format(0)
|
|||||||
}
|
}
|
||||||
|
|
||||||
<*
|
<*
|
||||||
Return a new String created using the formatting function.
|
Return a new String created using the formatting function, this function will implicitly
|
||||||
|
use the temp allocator.
|
||||||
|
|
||||||
@param [inout] allocator : `The allocator to use`
|
@param [inout] allocator : `The allocator to use`
|
||||||
@param [in] fmt : `The formatting string`
|
@param [in] fmt : `The formatting string`
|
||||||
@@ -156,29 +159,34 @@ macro bool char_in_set(char c, String set)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
<*
|
||||||
|
@return "a String which is safe to convert to a ZString"
|
||||||
|
*>
|
||||||
fn String join(Allocator allocator, String[] s, String joiner)
|
fn String join(Allocator allocator, String[] s, String joiner)
|
||||||
{
|
{
|
||||||
if (!s)
|
if (!s)
|
||||||
{
|
{
|
||||||
return (String)allocator::new_array(allocator, char, 2)[:0];
|
return (String)allocator::new_array(allocator, char, 2)[:0];
|
||||||
}
|
}
|
||||||
|
usz joiner_len = joiner.len;
|
||||||
usz total_size = joiner.len * s.len;
|
usz total_size = joiner_len * (s.len - 1) + 1;
|
||||||
foreach (String* &str : s)
|
foreach (String* &str : s)
|
||||||
{
|
{
|
||||||
total_size += str.len;
|
total_size += str.len;
|
||||||
}
|
}
|
||||||
@pool()
|
char[] data = allocator::alloc_array(allocator, char, total_size);
|
||||||
|
usz offset = s[0].len;
|
||||||
|
data[:offset] = s[0][:offset];
|
||||||
|
foreach (String* &str : s[1..])
|
||||||
{
|
{
|
||||||
DString res = dstring::temp_with_capacity(total_size);
|
data[offset:joiner_len] = joiner[:joiner_len];
|
||||||
res.append(s[0]);
|
offset += joiner_len;
|
||||||
foreach (String* &str : s[1..])
|
usz len = str.len;
|
||||||
{
|
data[offset:len] = str.[:len];
|
||||||
res.append(joiner);
|
offset += len;
|
||||||
res.append(*str);
|
|
||||||
}
|
|
||||||
return res.copy_str(allocator);
|
|
||||||
};
|
};
|
||||||
|
data[offset] = 0;
|
||||||
|
return (String)data[:offset];
|
||||||
}
|
}
|
||||||
|
|
||||||
<*
|
<*
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
### Stdlib changes
|
### Stdlib changes
|
||||||
- Summarize sort macros as generic function wrappers to reduce the amount of generated code. #2831
|
- Summarize sort macros as generic function wrappers to reduce the amount of generated code. #2831
|
||||||
|
- Remove dependency on temp allocator in String.join.
|
||||||
|
|
||||||
### Fixes
|
### Fixes
|
||||||
- Add error message if directory with output file name already exists
|
- Add error message if directory with output file name already exists
|
||||||
|
|||||||
Reference in New Issue
Block a user