1 module hunt.time.format.CharLiteralPrinterParser;
2 
3 import hunt.time.format.DateTimeParseContext;
4 import hunt.time.format.DateTimePrinterParser;
5 import hunt.time.format.DateTimePrintContext;
6 import hunt.time.temporal.TemporalField;
7 import hunt.util.StringBuilder;
8 
9 import std.string;
10 
11 //-----------------------------------------------------------------------
12 /**
13  * Prints or parses a character literal.
14  */
15 static final class CharLiteralPrinterParser : DateTimePrinterParser
16 {
17     private char literal;
18 
19     this(char literal)
20     {
21         this.literal = literal;
22     }
23 
24     override public bool format(DateTimePrintContext context, StringBuilder buf)
25     {
26         buf.append(literal);
27         return true;
28     }
29 
30     override public int parse(DateTimeParseContext context, string text, int position)
31     {
32         int length = cast(int)(text.length);
33         if (position == length)
34         {
35             return ~position;
36         }
37         char ch = text[position];
38         if (ch != literal)
39         {
40             if (context.isCaseSensitive() || (toUpper(ch) != toUpper(literal)
41                     && toUpper(ch) != toUpper(literal)))
42             {
43                 return ~position;
44             }
45         }
46         return position + 1;
47     }
48 
49     override public string toString()
50     {
51         if (literal == '\'')
52         {
53             return "''";
54         }
55         return "'" ~ literal ~ "'";
56     }
57 }