1 /* 2 * hunt-time: A time library for D programming language. 3 * 4 * Copyright (C) 2015-2018 HuntLabs 5 * 6 * Website: https://www.huntlabs.net/ 7 * 8 * Licensed under the Apache-2.0 License. 9 * 10 */ 11 12 module hunt.time.format.SignStyle; 13 14 /** 15 * Enumeration of ways to handle the positive/negative sign. 16 * !(p) 17 * The formatting engine allows the positive and negative signs of numbers 18 * to be controlled using this enum. 19 * See {@link DateTimeFormatterBuilder} for usage. 20 * 21 * @implSpec 22 * This is an immutable and thread-safe enum. 23 * 24 * @since 1.8 25 */ 26 public struct SignStyle { 27 28 /** 29 * Style to output the sign only if the value is negative. 30 * !(p) 31 * In strict parsing, the negative sign will be accepted and the positive sign rejected. 32 * In lenient parsing, any sign will be accepted. 33 */ 34 static SignStyle NORMAL = SignStyle(0,"NORMAL"); 35 /** 36 * Style to always output the sign, where zero will output '+'. 37 * !(p) 38 * In strict parsing, the absence of a sign will be rejected. 39 * In lenient parsing, any sign will be accepted, with the absence 40 * of a sign treated as a positive number. 41 */ 42 static SignStyle ALWAYS= SignStyle(1,"ALWAYS"); 43 /** 44 * Style to never output sign, only outputting the absolute value. 45 * !(p) 46 * In strict parsing, any sign will be rejected. 47 * In lenient parsing, any sign will be accepted unless the width is fixed. 48 */ 49 static SignStyle NEVER= SignStyle(2,"NEVER"); 50 /** 51 * Style to block negative values, throwing an exception on printing. 52 * !(p) 53 * In strict parsing, any sign will be rejected. 54 * In lenient parsing, any sign will be accepted unless the width is fixed. 55 */ 56 static SignStyle NOT_NEGATIVE= SignStyle(3,"NOT_NEGATIVE"); 57 /** 58 * Style to always output the sign if the value exceeds the pad width. 59 * A negative value will always output the '-' sign. 60 * !(p) 61 * In strict parsing, the sign will be rejected unless the pad width is exceeded. 62 * In lenient parsing, any sign will be accepted, with the absence 63 * of a sign treated as a positive number. 64 */ 65 static SignStyle EXCEEDS_PAD= SignStyle(4,"EXCEEDS_PAD"); 66 67 private string _name; 68 private int _ordinal; 69 public int ordinal() 70 { 71 return _ordinal; 72 } 73 public string name() 74 { 75 return _name; 76 } 77 this(int ordinal ,string name) 78 { 79 _ordinal = ordinal; 80 _name = name; 81 } 82 /** 83 * Parse helper. 84 * 85 * @param positive true if positive sign parsed, false for negative sign 86 * @param strict true if strict, false if lenient 87 * @param fixedWidth true if fixed width, false if not 88 * @return 89 */ 90 bool parse(bool positive, bool strict, bool fixedWidth) { 91 switch (ordinal()) { 92 case 0: // NORMAL 93 // valid if negative or (positive and lenient) 94 return !positive || !strict; 95 case 1: // ALWAYS 96 case 4: // EXCEEDS_PAD 97 return true; 98 default: 99 // valid if lenient and not fixed width 100 return !strict && !fixedWidth; 101 } 102 } 103 104 }