[california/wip/731723-locale] Remove leading zeroes from pretty date formats



commit a3be008da488fcbcd01a7956a6dfb046100f1a66
Author: Jim Nelson <jim yorba org>
Date:   Wed Jun 18 18:16:44 2014 -0700

    Remove leading zeroes from pretty date formats
    
    Required because some strftime formatting adds a leading zero, always.

 src/Makefile.am                 |    1 +
 src/calendar/calendar-date.vala |    3 +-
 src/tests/tests-string.vala     |   44 +++++++++++++++++++++++++++++++++++++++
 src/tests/tests.vala            |    1 +
 src/util/util-string.vala       |   36 +++++++++++++++++++++++++++++++
 5 files changed, 84 insertions(+), 1 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 2b50cad..7498275 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -107,6 +107,7 @@ california_VALASOURCES = \
        tests/tests-calendar-month-span.vala \
        tests/tests-calendar-wall-time.vala \
        tests/tests-quick-add.vala \
+       tests/tests-string.vala \
        \
        toolkit/toolkit.vala \
        toolkit/toolkit-button-connector.vala \
diff --git a/src/calendar/calendar-date.vala b/src/calendar/calendar-date.vala
index ab8c29e..0ac0d9c 100644
--- a/src/calendar/calendar-date.vala
+++ b/src/calendar/calendar-date.vala
@@ -369,7 +369,8 @@ public class Date : Unit<Date>, Gee.Comparable<Date>, Gee.Hashable<Date> {
                 fmt = with_year ? FMT_PRETTY_DATE : FMT_PRETTY_DATE_NO_YEAR;
         }
         
-        return String.reduce_whitespace(format(fmt));
+        // Return string with leading zeros and extraneous whitespace removed
+        return String.reduce_whitespace(String.remove_leading_chars(format(fmt), '0'));
     }
     
     public override string to_string() {
diff --git a/src/tests/tests-string.vala b/src/tests/tests-string.vala
new file mode 100644
index 0000000..436cf81
--- /dev/null
+++ b/src/tests/tests-string.vala
@@ -0,0 +1,44 @@
+/* Copyright 2014 Yorba Foundation
+ *
+ * This software is licensed under the GNU Lesser General Public License
+ * (version 2.1 or later).  See the COPYING file in this distribution.
+ */
+
+namespace California.Tests {
+
+private class String : UnitTest.Harness {
+    public String() {
+        add_case("strip-zeroes-space", strip_zeroes_space);
+        add_case("strip-zeroes-slash", strip_zeroes_slash);
+        add_case("strip-zeroes-multiple", strip_zeroes_multiple);
+    }
+    
+    protected override void setup() throws Error {
+        Util.init();
+    }
+    
+    protected override void teardown() {
+        Util.terminate();
+    }
+    
+    private bool strip_zeroes_space() throws Error {
+        string result = California.String.remove_leading_chars("01 2 03 4", '0');
+        
+        return result == "1 2 3 4";
+    }
+    
+    private bool strip_zeroes_slash() throws Error {
+        string result = California.String.remove_leading_chars("01/2/03/4", '0', " /");
+        
+        return result == "1/2/3/4";
+    }
+    
+    private bool strip_zeroes_multiple() throws Error {
+        string result = California.String.remove_leading_chars("001/2/03/4", '0', " /");
+        
+        return result == "1/2/3/4";
+    }
+}
+
+}
+
diff --git a/src/tests/tests.vala b/src/tests/tests.vala
index 1d484b0..c85530b 100644
--- a/src/tests/tests.vala
+++ b/src/tests/tests.vala
@@ -7,6 +7,7 @@
 namespace California.Tests {
 
 public int run(string[] args) {
+    UnitTest.Harness.register(new String());
     UnitTest.Harness.register(new QuickAdd());
     UnitTest.Harness.register(new CalendarDate());
     UnitTest.Harness.register(new CalendarMonthSpan());
diff --git a/src/util/util-string.vala b/src/util/util-string.vala
index b9561af..05b9c33 100644
--- a/src/util/util-string.vala
+++ b/src/util/util-string.vala
@@ -69,5 +69,41 @@ public bool is_numeric(string? str) {
     return true;
 }
 
+/**
+ * Removes leading characters from throughout the string.
+ *
+ * Both the leading character and what constitutes tokens can be specified.
+ *
+ * Results are undefined if the leading character is also found in the delimiter string.
+ */
+public string? remove_leading_chars(string? str, unichar ch, string delims = " ") {
+    if (is_empty(str))
+        return str;
+    
+    StringBuilder builder = new StringBuilder();
+    unichar current_ch;
+    int index = 0;
+    bool leading = true;
+    while (str.get_next_char(ref index, out current_ch)) {
+        // if character is a delimiter, reset state, append, and move on
+        if (delims.index_of_char(current_ch) >= 0) {
+            leading = true;
+            builder.append_unichar(current_ch);
+            
+            continue;
+        }
+        
+        // if looking for leading characters and this matches, drop on the floor
+        if (leading && current_ch == ch)
+            continue;
+        
+        // done looking for leading characters until the next delimiter
+        leading = false;
+        builder.append_unichar(current_ch);
+    }
+    
+    return builder.str;
+}
+
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]