Skip to content

Commit 9e56a82

Browse files
committed
fix string map and add toString
1 parent d446eca commit 9e56a82

File tree

3 files changed

+37
-11
lines changed

3 files changed

+37
-11
lines changed

source/mir/algebraic_alias/json.d

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,8 @@ unittest
124124

125125
JsonAlgebraic[string] aa = object.toAA;
126126
object = StringMap!JsonAlgebraic(aa);
127+
128+
JsonAlgebraic fromAA = ["a" : JsonAlgebraic(3), "b" : JsonAlgebraic("b")];
129+
assert(fromAA.get!(StringMap!JsonAlgebraic)["a"] == 3);
130+
assert(fromAA.get!(StringMap!JsonAlgebraic)["b"] == "b");
127131
}

source/mir/serde.d

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -529,8 +529,6 @@ alias serdeGetIgnoreOutIf(alias symbol) = naryFun!(TemplateArgsOf!(getUDA!(symbo
529529
/++
530530
Attributes to conditional ignore field during serialization.
531531
532-
The attribute can be combined with $(LREF serdeOrderedIn), $(LREF serdeRealOrderedIn).
533-
534532
The predicate should be aplied to the aggregate value, not to the member.
535533
536534
See_also: $(LREF serdeIgnoreIfAggregate) $(LREF serdeIgnoreOutIf), $(LREF serdeIgnoreInIfAggregate)
@@ -544,7 +542,7 @@ alias serdeGetIgnoreOutIfAggregate(alias symbol) = naryFun!(TemplateArgsOf!(getU
544542
/++
545543
Attributes to conditional ignore field during deserialization.
546544
547-
The predicate should be aplied to the aggregate value, not to the member.
545+
The attribute should be combined with $(LREF serdeRealOrderedIn) applied on the aggregate.
548546
549547
See_also: $(LREF serdeIgnoreIfAggregate) $(LREF serdeIgnoreOutIfAggregate) $(LREF serdeIgnoreIn)
550548
+/
@@ -557,7 +555,7 @@ alias serdeGetIgnoreInIfAggregate(alias symbol) = naryFun!(TemplateArgsOf!(getUD
557555
/++
558556
Attributes to conditional ignore field during serialization and deserialization.
559557
560-
The attribute can be combined with $(LREF serdeOrderedIn), $(LREF serdeRealOrderedIn).
558+
The attribute should be combined with $(LREF serdeRealOrderedIn) applied on the aggregate.
561559
562560
The predicate should be aplied to the aggregate value, not to the member.
563561

source/mir/string_map.d

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,34 @@ struct StringMap(T, U = uint)
111111
assert(map.findPos("key") == 0);
112112
}
113113

114+
///
115+
string toString() const
116+
{
117+
import mir.format: stringBuf;
118+
stringBuf buffer;
119+
toString(buffer);
120+
return buffer.data.idup;
121+
}
122+
123+
///ditto
124+
void toString(W)(scope ref W w) const
125+
{
126+
bool next;
127+
w.put('[');
128+
import mir.format: printEscaped, EscapeFormat, print;
129+
foreach (i, ref value; values)
130+
{
131+
if (next)
132+
w.put(`, `);
133+
next = true;
134+
w.put('\"');
135+
printEscaped!(char, EscapeFormat.ion)(w, keys[i]);
136+
w.put(`": `);
137+
print(w, value);
138+
}
139+
w.put(']');
140+
}
141+
114142
/++
115143
Constructs an associative array using keys and values.
116144
Params:
@@ -704,7 +732,7 @@ private struct StructImpl(T, U = uint)
704732
this()(string[] keys, T[] values) @trusted pure nothrow
705733
{
706734
import mir.array.allocation: array;
707-
import mir.ndslice.sorting: sort;
735+
import mir.ndslice.sorting: makeIndex;
708736
import mir.ndslice.topology: iota, indexed;
709737
import mir.string_table: smallerStringFirst;
710738

@@ -714,13 +742,9 @@ private struct StructImpl(T, U = uint)
714742
_length = keys.length;
715743
_keys = keys.ptr;
716744
_values = values.ptr;
717-
_indices = keys.length.iota!U.array.ptr;
745+
_indices = keys.makeIndex!(U, smallerStringFirst).ptr;
718746
auto sortedKeys = _keys.indexed(indices);
719-
sortedKeys.sort!smallerStringFirst;
720-
size_t maxKeyLength;
721-
foreach (ref key; keys)
722-
if (key.length > maxKeyLength)
723-
maxKeyLength = key.length;
747+
size_t maxKeyLength = sortedKeys[$ - 1].length;
724748
_lengthTable = new U[maxKeyLength + 2];
725749

726750
size_t ski;

0 commit comments

Comments
 (0)