[california] Remove repititious whitespace in prettified time/date strings



commit 406a149a786bb3756911058bbdd2acd72af55c1f
Author: Jim Nelson <jim yorba org>
Date:   Tue Apr 15 19:25:47 2014 -0700

    Remove repititious whitespace in prettified time/date strings
    
    Some of the strftime format specifiers will issue leading whitespace
    for single-digit values.  This removes that extraneous whitespace.

 src/calendar/calendar-date.vala      |    2 +-
 src/calendar/calendar-wall-time.vala |    8 ++++----
 src/util/util-string.vala            |   28 ++++++++++++++++++++++++++++
 3 files changed, 33 insertions(+), 5 deletions(-)
---
diff --git a/src/calendar/calendar-date.vala b/src/calendar/calendar-date.vala
index 65b7b2c..c9166b0 100644
--- a/src/calendar/calendar-date.vala
+++ b/src/calendar/calendar-date.vala
@@ -299,7 +299,7 @@ public class Date : BaseObject, Gee.Comparable<Date>, Gee.Hashable<Date> {
         else
             fmt = with_year ? FMT_PRETTY_DATE : FMT_PRETTY_DATE_NO_YEAR;
         
-        return format(fmt);
+        return String.reduce_whitespace(format(fmt));
     }
     
     public override string to_string() {
diff --git a/src/calendar/calendar-wall-time.vala b/src/calendar/calendar-wall-time.vala
index ff93fae..7eecb7b 100644
--- a/src/calendar/calendar-wall-time.vala
+++ b/src/calendar/calendar-wall-time.vala
@@ -286,16 +286,16 @@ public class WallTime : BaseObject, Gee.Comparable<WallTime>, Gee.Hashable<WallT
         if (!include_sec) {
             // hour and minutes only
             if (is_24hr)
-                return FMT_24HOUR_MIN.printf(hour, minute);
+                return String.reduce_whitespace(FMT_24HOUR_MIN.printf(hour, minute));
             
-            return FMT_12HOUR_MIN_MERIDIEM.printf(12hour, minute, meridiem);
+            return String.reduce_whitespace(FMT_12HOUR_MIN_MERIDIEM.printf(12hour, minute, meridiem));
         }
         
         // the full package
         if (is_24hr)
-            return FMT_24HOUR_MIN_SEC.printf(hour, minute, second);
+            return String.reduce_whitespace(FMT_24HOUR_MIN_SEC.printf(hour, minute, second));
         
-        return FMT_12HOUR_MIN_SEC_MERIDIEM.printf(12hour, minute, second, meridiem);
+        return String.reduce_whitespace(FMT_12HOUR_MIN_SEC_MERIDIEM.printf(12hour, minute, second, 
meridiem));
     }
     
     public int compare_to(WallTime other) {
diff --git a/src/util/util-string.vala b/src/util/util-string.vala
index 070c9d3..693cb11 100644
--- a/src/util/util-string.vala
+++ b/src/util/util-string.vala
@@ -16,5 +16,33 @@ public int stricmp(string a, string b) {
     return strcmp(a.casefold(), b.casefold());
 }
 
+/**
+ * Removes redundant whitespace (including tabs and newlines) and strips whitespace from beginning
+ * and end of string.
+ */
+public string reduce_whitespace(string str) {
+    if (str[0] == NUL)
+        return str;
+    
+    StringBuilder builder = new StringBuilder();
+    unichar ch;
+    unichar last_ch = NUL;
+    int index = 0;
+    while (str.get_next_char(ref index, out ch)) {
+        // if space but last char not NUL (i.e. this is not the first character, a space) and the
+        // last char was not a space, append, otherwise drop
+        if (ch.isspace()) {
+            if (last_ch != NUL && !last_ch.isspace())
+                builder.append_unichar(ch);
+        } else {
+            builder.append_unichar(ch);
+        }
+        
+        last_ch = ch;
+    }
+    
+    return builder.str;
+}
+
 }
 


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