Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dub.sdl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors "Ilya Yaroshenko" "John Michael Hall" "Shigeki Karita" "Sebastian Wilzba
copyright "2020 Ilya Yaroshenko, Kaleidic Associates Advisory Limited, Symmetry Investments"
license "Apache-2.0"

dependency "mir-core" version=">=1.1.54"
dependency "mir-core" version=">=1.1.67"

buildType "unittest" {
buildOptions "unittests" "debugMode" "debugInfo"
Expand Down
63 changes: 61 additions & 2 deletions source/mir/date.d
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Authors: $(HTTP jmdavisprog.com, Jonathan M Davis), Ilya Yaroshenko (boost-like
+/
module mir.date;

import mir.timestamp: Timestamp;
import mir.serde: serdeProxy, serdeScoped;
import std.range.primitives : isOutputRange;
import std.traits : isSomeChar, Unqual;
Expand Down Expand Up @@ -243,12 +244,55 @@ enum DayOfWeek
}

///
@serdeProxy!Timestamp
struct YearMonthDay
{
short year = 1;
Month month = Month.jan;
ubyte day = 1;

///
Timestamp timestamp() @safe pure nothrow @nogc @property
{
return Timestamp(year, cast(ubyte)month, day);
}

///
alias opCast(T : Timestamp) = timestamp;

///
version(mir_test)
unittest
{
import mir.timestamp;
auto timestamp = cast(Timestamp) YearMonthDay(2020, Month.may, 12);
}

///
this(short year, Month month, ubyte day) @safe pure nothrow @nogc
{
this.year = year;
this.month = month;
this.day = day;
}

///
this(Date date) @safe pure nothrow @nogc
{
this = date.yearMonthDay;
}

version(D_Exceptions)
///
this(Timestamp timestamp) @safe pure nothrow @nogc
{
if (timestamp.precision != Timestamp.Precision.day)
{
static immutable exc = new Exception("YearMonthDay: invalid timestamp precision");
}
with(timestamp) this(year, cast(Month)month, day);
}

// Shares documentation with "years" version.
@safe pure nothrow @nogc
ref YearMonthDay add(string units)(long months, AllowDayOverflow allowOverflow = AllowDayOverflow.yes)
Expand Down Expand Up @@ -410,8 +454,7 @@ struct YearMonthDay
+/
extern(C++, "boost", "gregorian")
extern(C++, class)
@serdeScoped
@serdeProxy!(const(char)[])
@serdeProxy!YearMonthDay
struct date
{
extern(D):
Expand Down Expand Up @@ -507,6 +550,22 @@ public:
return ret;
}

///
Timestamp timestamp() @safe pure nothrow @nogc @property
{
return yearMonthDay.timestamp;
}

version(D_Exceptions)
///
this(Timestamp timestamp) @safe pure @nogc
{
if (timestamp.precision != Timestamp.Precision.day)
{
static immutable exc = new Exception("Date: invalid timestamp precision");
}
}

version(D_Exceptions)
///
this(scope const(char)[] str) @safe pure @nogc
Expand Down
61 changes: 46 additions & 15 deletions source/mir/format.d
Original file line number Diff line number Diff line change
Expand Up @@ -334,11 +334,17 @@ struct HexAddress(T)
}

/++
Escaped string formats
+/
enum EscapeFormat
{
/// JSON escaped string format
json,
/// Amzn Ion CLOB format
ionClob,
/// Amzn Ion symbol format
ionSymbol,
/// Amzn Ion string format
ion,
}

Expand All @@ -355,6 +361,11 @@ ref W printEscaped(C, EscapeFormat escapeFormat = EscapeFormat.ion, W)(scope ret
goto E;
if (_expect(c < ' ', false))
goto C;
static if (escapeFormat == EscapeFormat.ionClob)
{
if (c >= 127)
goto A;
}
P:
w.put(c);
continue;
Expand All @@ -367,22 +378,42 @@ ref W printEscaped(C, EscapeFormat escapeFormat = EscapeFormat.ion, W)(scope ret
continue;
}
C:
if (c == '\t' || c == '\f' || c == '\b')
goto P;
if (c == '\n')
{
c = 'n';
goto E;
}
if (c == '\r')
switch (c)
{
c = 'r';
goto E;
static if (escapeFormat != EscapeFormat.json)
{
case '\0':
c = '0';
goto E;
case '\a':
c = 'a';
goto E;
case '\v':
c = 'v';
goto E;
}
case '\b':
c = 'b';
goto E;
case '\t':
c = 't';
goto E;
case '\n':
c = 'n';
goto E;
case '\f':
c = 'f';
goto E;
case '\r':
c = 'r';
goto E;
default:
A:
static if (escapeFormat == EscapeFormat.json)
put_uXXXX!C(w, cast(char)c);
else
put_xXX!C(w, cast(char)c);
}
static if (escapeFormat == EscapeFormat.json)
put_uXXXX!C(w, cast(char)c);
else
put_xXX!C(w, cast(char)c);
}
return w;
}
Expand All @@ -394,7 +425,7 @@ version (mir_test) unittest

import mir.format: stringBuf;
stringBuf w;
assert(w.printEscaped("Hi \f\t\b \\\r\n" ~ `"@nogc"`).data == "Hi \f\t\b \\\\\\r\\n\\\"@nogc\\\"", w.data);
assert(w.printEscaped("Hi \a\v\0\f\t\b \\\r\n" ~ `"@nogc"`).data == `Hi \a\v\0\f\t\b \\\r\n\"@nogc\"`);
w.reset;
assert(w.printEscaped("\x03").data == `\x03`, w.data);
}
Expand Down
Loading