1 module hunt.time.format.StringLiteralPrinterParser;
2 
3 import hunt.time.chrono.Chronology;
4 import hunt.time.format.DateTimeParseContext;
5 import hunt.time.format.DateTimePrinterParser;
6 import hunt.time.format.DateTimePrintContext;
7 import hunt.time.format.DateTimeTextProvider;
8 import hunt.time.format.TextStyle;
9 import hunt.time.temporal.TemporalField;
10 
11 import hunt.text.Common;
12 import hunt.util.StringBuilder;
13 
14 import hunt.Exceptions;
15 import hunt.Long;
16 
17 import std.string;
18 
19 //-----------------------------------------------------------------------
20 /**
21  * Prints or parses a string literal.
22  */
23 static final class StringLiteralPrinterParser : DateTimePrinterParser
24 {
25     private string literal;
26 
27     this(string literal)
28     {
29         this.literal = literal; // validated by caller
30     }
31 
32     override public bool format(DateTimePrintContext context, StringBuilder buf)
33     {
34         buf.append(literal);
35         return true;
36     }
37 
38     override public int parse(DateTimeParseContext context, string text, int position)
39     {
40         int length = cast(int)(text.length);
41         if (position > length || position < 0)
42         {
43             throw new IndexOutOfBoundsException();
44         }
45         if (context.subSequenceEquals(text, position, literal, 0,
46                 cast(int)(literal.length)) == false)
47         {
48             return ~position;
49         }
50         return position + cast(int)(literal.length);
51     }
52 
53     override public string toString()
54     {
55         string converted = literal.replace("'", "''");
56         return "'" ~ converted ~ "'";
57     }
58 }
59