Skip to content

Commit 3b8b2c4

Browse files
authored
prepare for Ion (#343)
* prepare for Ion * update mir.serde for Ion * update dub
1 parent a41193f commit 3b8b2c4

4 files changed

Lines changed: 340 additions & 34 deletions

File tree

dub.sdl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ authors "Ilya Yaroshenko" "John Michael Hall" "Shigeki Karita" "Sebastian Wilzba
55
copyright "2020 Ilya Yaroshenko, Kaleidic Associates Advisory Limited, Symmetry Investments"
66
license "Apache-2.0"
77

8-
dependency "mir-core" version=">=1.1.54"
8+
dependency "mir-core" version=">=1.1.67"
99

1010
buildType "unittest" {
1111
buildOptions "unittests" "debugMode" "debugInfo"

source/mir/date.d

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Authors: $(HTTP jmdavisprog.com, Jonathan M Davis), Ilya Yaroshenko (boost-like
2929
+/
3030
module mir.date;
3131

32+
import mir.timestamp: Timestamp;
3233
import mir.serde: serdeProxy, serdeScoped;
3334
import std.range.primitives : isOutputRange;
3435
import std.traits : isSomeChar, Unqual;
@@ -243,12 +244,55 @@ enum DayOfWeek
243244
}
244245

245246
///
247+
@serdeProxy!Timestamp
246248
struct YearMonthDay
247249
{
248250
short year = 1;
249251
Month month = Month.jan;
250252
ubyte day = 1;
251253

254+
///
255+
Timestamp timestamp() @safe pure nothrow @nogc @property
256+
{
257+
return Timestamp(year, cast(ubyte)month, day);
258+
}
259+
260+
///
261+
alias opCast(T : Timestamp) = timestamp;
262+
263+
///
264+
version(mir_test)
265+
unittest
266+
{
267+
import mir.timestamp;
268+
auto timestamp = cast(Timestamp) YearMonthDay(2020, Month.may, 12);
269+
}
270+
271+
///
272+
this(short year, Month month, ubyte day) @safe pure nothrow @nogc
273+
{
274+
this.year = year;
275+
this.month = month;
276+
this.day = day;
277+
}
278+
279+
///
280+
this(Date date) @safe pure nothrow @nogc
281+
{
282+
this = date.yearMonthDay;
283+
}
284+
285+
version(D_Exceptions)
286+
///
287+
this(Timestamp timestamp) @safe pure nothrow @nogc
288+
{
289+
if (timestamp.precision != Timestamp.Precision.day)
290+
{
291+
static immutable exc = new Exception("YearMonthDay: invalid timestamp precision");
292+
}
293+
with(timestamp) this(year, cast(Month)month, day);
294+
}
295+
252296
// Shares documentation with "years" version.
253297
@safe pure nothrow @nogc
254298
ref YearMonthDay add(string units)(long months, AllowDayOverflow allowOverflow = AllowDayOverflow.yes)
@@ -410,8 +454,7 @@ struct YearMonthDay
410454
+/
411455
extern(C++, "boost", "gregorian")
412456
extern(C++, class)
413-
@serdeScoped
414-
@serdeProxy!(const(char)[])
457+
@serdeProxy!YearMonthDay
415458
struct date
416459
{
417460
extern(D):
@@ -507,6 +550,22 @@ public:
507550
return ret;
508551
}
509552

553+
///
554+
Timestamp timestamp() @safe pure nothrow @nogc @property
555+
{
556+
return yearMonthDay.timestamp;
557+
}
558+
559+
version(D_Exceptions)
560+
///
561+
this(Timestamp timestamp) @safe pure @nogc
562+
{
563+
if (timestamp.precision != Timestamp.Precision.day)
564+
{
565+
static immutable exc = new Exception("Date: invalid timestamp precision");
566+
}
567+
}
568+
510569
version(D_Exceptions)
511570
///
512571
this(scope const(char)[] str) @safe pure @nogc

source/mir/format.d

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -334,11 +334,17 @@ struct HexAddress(T)
334334
}
335335

336336
/++
337+
Escaped string formats
337338
+/
338339
enum EscapeFormat
339340
{
341+
/// JSON escaped string format
340342
json,
343+
/// Amzn Ion CLOB format
344+
ionClob,
345+
/// Amzn Ion symbol format
341346
ionSymbol,
347+
/// Amzn Ion string format
342348
ion,
343349
}
344350

@@ -355,6 +361,11 @@ ref W printEscaped(C, EscapeFormat escapeFormat = EscapeFormat.ion, W)(scope ret
355361
goto E;
356362
if (_expect(c < ' ', false))
357363
goto C;
364+
static if (escapeFormat == EscapeFormat.ionClob)
365+
{
366+
if (c >= 127)
367+
goto A;
368+
}
358369
P:
359370
w.put(c);
360371
continue;
@@ -367,22 +378,42 @@ ref W printEscaped(C, EscapeFormat escapeFormat = EscapeFormat.ion, W)(scope ret
367378
continue;
368379
}
369380
C:
370-
if (c == '\t' || c == '\f' || c == '\b')
371-
goto P;
372-
if (c == '\n')
373-
{
374-
c = 'n';
375-
goto E;
376-
}
377-
if (c == '\r')
381+
switch (c)
378382
{
379-
c = 'r';
380-
goto E;
383+
static if (escapeFormat != EscapeFormat.json)
384+
{
385+
case '\0':
386+
c = '0';
387+
goto E;
388+
case '\a':
389+
c = 'a';
390+
goto E;
391+
case '\v':
392+
c = 'v';
393+
goto E;
394+
}
395+
case '\b':
396+
c = 'b';
397+
goto E;
398+
case '\t':
399+
c = 't';
400+
goto E;
401+
case '\n':
402+
c = 'n';
403+
goto E;
404+
case '\f':
405+
c = 'f';
406+
goto E;
407+
case '\r':
408+
c = 'r';
409+
goto E;
410+
default:
411+
A:
412+
static if (escapeFormat == EscapeFormat.json)
413+
put_uXXXX!C(w, cast(char)c);
414+
else
415+
put_xXX!C(w, cast(char)c);
381416
}
382-
static if (escapeFormat == EscapeFormat.json)
383-
put_uXXXX!C(w, cast(char)c);
384-
else
385-
put_xXX!C(w, cast(char)c);
386417
}
387418
return w;
388419
}
@@ -394,7 +425,7 @@ version (mir_test) unittest
394425

395426
import mir.format: stringBuf;
396427
stringBuf w;
397-
assert(w.printEscaped("Hi \f\t\b \\\r\n" ~ `"@nogc"`).data == "Hi \f\t\b \\\\\\r\\n\\\"@nogc\\\"", w.data);
428+
assert(w.printEscaped("Hi \a\v\0\f\t\b \\\r\n" ~ `"@nogc"`).data == `Hi \a\v\0\f\t\b \\\r\n\"@nogc\"`);
398429
w.reset;
399430
assert(w.printEscaped("\x03").data == `\x03`, w.data);
400431
}

0 commit comments

Comments
 (0)