mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
Added some to_string.
This commit is contained in:
@@ -13,6 +13,23 @@ fn void EnumMap.init(EnumMap* this, ValueType init_value)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn void! EnumMap.to_format(EnumMap* map, Formatter* formatter) @dynamic
|
||||
{
|
||||
formatter.print("{ ")!;
|
||||
foreach (i, &value : map.values)
|
||||
{
|
||||
if (i != 0) formatter.print(", ")!;
|
||||
formatter.printf("%s: %s", (Enum)i, *value)!;
|
||||
}
|
||||
formatter.print(" }")!;
|
||||
}
|
||||
|
||||
fn String EnumMap.to_string(EnumMap* map, Allocator* using = mem::heap()) @dynamic
|
||||
{
|
||||
return string::printf("%s", *map);
|
||||
}
|
||||
|
||||
fn uint EnumMap.len(EnumMap* this) @operator(len) => this.values.len;
|
||||
fn ValueType EnumMap.get(EnumMap* this, Enum key) @operator([]) => this.values[key.ordinal];
|
||||
fn void EnumMap.set(EnumMap* this, Enum key, ValueType value) @operator([]=) => this.values[key.ordinal] = value;
|
||||
|
||||
@@ -12,7 +12,6 @@ def EnumSetType = $typefrom(private::type_for_enum_elements(Enum.elements)) @pri
|
||||
const IS_CHAR_ARRAY = Enum.elements > 128;
|
||||
def EnumSet = distinct EnumSetType;
|
||||
|
||||
|
||||
fn void EnumSet.add(EnumSet* this, Enum v)
|
||||
{
|
||||
$if IS_CHAR_ARRAY:
|
||||
@@ -126,6 +125,25 @@ fn EnumSet EnumSet.xor_of(EnumSet* this, EnumSet s)
|
||||
$endif
|
||||
}
|
||||
|
||||
fn void! EnumSet.to_format(EnumSet* set, Formatter* formatter) @dynamic
|
||||
{
|
||||
formatter.print("[")!;
|
||||
bool found = false;
|
||||
foreach (value : Enum.values)
|
||||
{
|
||||
if (!set.has(value)) continue;
|
||||
if (found) formatter.print(", ")!;
|
||||
found = true;
|
||||
formatter.printf("%s", value)!;
|
||||
}
|
||||
formatter.print("]")!;
|
||||
}
|
||||
|
||||
fn String EnumSet.to_string(EnumSet* set, Allocator* using = mem::heap()) @dynamic
|
||||
{
|
||||
return string::printf("%s", *set);
|
||||
}
|
||||
|
||||
module std::collections::enumset::private;
|
||||
|
||||
macro typeid type_for_enum_elements(usz $elements)
|
||||
|
||||
@@ -195,6 +195,8 @@ fn Key[] HashMap.key_list(HashMap* map, Allocator* using = mem::heap())
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
|
||||
fn Value[] HashMap.value_tlist(HashMap* map)
|
||||
{
|
||||
return map.value_list(mem::temp()) @inline;
|
||||
|
||||
@@ -111,3 +111,13 @@ fn Type PriorityQueue.peek_at(PriorityQueue* pq, usz index) @operator([])
|
||||
{
|
||||
return pq.heap[index];
|
||||
}
|
||||
|
||||
fn void! PriorityQueue.to_format(PriorityQueue* list, Formatter* formatter) @dynamic
|
||||
{
|
||||
return list.heap.to_format(formatter);
|
||||
}
|
||||
|
||||
fn String PriorityQueue.to_string(PriorityQueue* list, Allocator* using = mem::heap()) @dynamic
|
||||
{
|
||||
return list.heap.to_string(formatter);
|
||||
}
|
||||
|
||||
@@ -24,6 +24,16 @@ fn Type Range.get(Range* range, usz index) @operator([])
|
||||
return range.start + (Type)index;
|
||||
}
|
||||
|
||||
fn String Range.to_string(Range* list, Allocator* using = mem::heap()) @dynamic
|
||||
{
|
||||
return string::printf("[%s..%s]", list.start, list.end);
|
||||
}
|
||||
|
||||
fn void! Range.to_format(Range* list, Formatter* formatter) @dynamic
|
||||
{
|
||||
formatter.printf("[%s..%s]", list.start, list.end)!;
|
||||
}
|
||||
|
||||
struct ExclusiveRange
|
||||
{
|
||||
Type start;
|
||||
@@ -36,6 +46,16 @@ fn usz ExclusiveRange.len(ExclusiveRange* range) @operator(len)
|
||||
return (usz)(range.end - range.start);
|
||||
}
|
||||
|
||||
fn void! ExclusiveRange.to_format(ExclusiveRange* list, Formatter* formatter) @dynamic
|
||||
{
|
||||
formatter.printf("[%s..<%s]", list.start, list.end)!;
|
||||
}
|
||||
|
||||
fn String ExclusiveRange.to_string(ExclusiveRange* list, Allocator* using = mem::heap()) @dynamic
|
||||
{
|
||||
return string::printf("[%s..<%s]", list.start, list.end);
|
||||
}
|
||||
|
||||
/**
|
||||
* @require index < range.len() : "Can't index into an empty range"
|
||||
**/
|
||||
|
||||
@@ -26,4 +26,5 @@ fn void! test_range()
|
||||
sum += z * z;
|
||||
}
|
||||
assert(sum == 35);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user