Hi Bigcheese, chandlerc,

Introduce a new countof template "macro" to determine the count of elements in
a statically allocated array.  Use this to remove the various static hardcoded
values for array element counts (strftime_l takes number of characters to write,
not the size of the buffer, though they are equivalent with char).

Provide an overload which takes two pointers (iterators) and returns the count
of elements that would fit in the array bounded by the iterators.  Do this to
avoid having to add a pointless static assertion along the lines of:

    static_assert(is_same<decltype(__ib),
                          typename 
remove_reference<decltype(__ie)>::type>::value,
                  "iterator type mismatch");

to ensure that the iterator types match when performing the calculation using
sizeof on either of the iterators to determine the size of a single element:

    (__ie - __ib) / sizeof(*__ib)


http://llvm-reviews.chandlerc.com/D245

Files:
  src/locale.cpp
Index: src/locale.cpp
===================================================================
--- src/locale.cpp
+++ src/locale.cpp
@@ -81,6 +81,18 @@
     return *(T*)&buf;
 }
 
+template <typename T, size_t N>
+_LIBCPP_ALWAYS_INLINE _LIBCPP_CONSTEXPR size_t countof(const T (&)[N])
+{
+    return N;
+}
+
+template <typename T>
+_LIBCPP_ALWAYS_INLINE size_t countof(const T * const begin, const T * const end)
+{
+    return (end - begin) / sizeof(T);
+}
+
 }
 
 const locale::category locale::none;
@@ -4598,7 +4610,7 @@
     char f[3] = {0};
     f[0] = '%';
     f[1] = fmt;
-    size_t n = strftime_l(buf, 100, f, &t, __loc_);
+    size_t n = strftime_l(buf, countof(buf), f, &t, __loc_);
     char* bb = buf;
     char* be = buf + n;
     string result;
@@ -4744,15 +4756,15 @@
     char f[3] = {0};
     f[0] = '%';
     f[1] = fmt;
-    strftime_l(buf, 100, f, &t, __loc_);
+    strftime_l(buf, countof(buf), f, &t, __loc_);
     wchar_t wbuf[100];
     wchar_t* wbb = wbuf;
     mbstate_t mb = {0,{0}};
     const char* bb = buf;
 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
-    size_t j = mbsrtowcs_l( wbb, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
+    size_t j = mbsrtowcs_l( wbb, &bb, countof(wbuf), &mb, __loc_);
 #else
-    size_t j = __mbsrtowcs_l( wbb, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
+    size_t j = __mbsrtowcs_l( wbb, &bb, countof(wbuf), &mb, __loc_);
 #endif
     if (j == size_t(-1))
         __throw_runtime_error("locale not supported");
@@ -4890,26 +4902,26 @@
     for (int i = 0; i < 7; ++i)
     {
         t.tm_wday = i;
-        strftime_l(buf, 100, "%A", &t, __loc_);
+        strftime_l(buf, countof(buf), "%A", &t, __loc_);
         __weeks_[i] = buf;
-        strftime_l(buf, 100, "%a", &t, __loc_);
+        strftime_l(buf, countof(buf), "%a", &t, __loc_);
         __weeks_[i+7] = buf;
     }
     // __months_
     for (int i = 0; i < 12; ++i)
     {
         t.tm_mon = i;
-        strftime_l(buf, 100, "%B", &t, __loc_);
+        strftime_l(buf, countof(buf), "%B", &t, __loc_);
         __months_[i] = buf;
-        strftime_l(buf, 100, "%b", &t, __loc_);
+        strftime_l(buf, countof(buf), "%b", &t, __loc_);
         __months_[i+12] = buf;
     }
     // __am_pm_
     t.tm_hour = 1;
-    strftime_l(buf, 100, "%p", &t, __loc_);
+    strftime_l(buf, countof(buf), "%p", &t, __loc_);
     __am_pm_[0] = buf;
     t.tm_hour = 13;
-    strftime_l(buf, 100, "%p", &t, __loc_);
+    strftime_l(buf, countof(buf), "%p", &t, __loc_);
     __am_pm_[1] = buf;
     __c_ = __analyze('c', ct);
     __r_ = __analyze('r', ct);
@@ -4930,25 +4942,25 @@
     for (int i = 0; i < 7; ++i)
     {
         t.tm_wday = i;
-        strftime_l(buf, 100, "%A", &t, __loc_);
+        strftime_l(buf, countof(buf), "%A", &t, __loc_);
         mb = mbstate_t();
         const char* bb = buf;
 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
-        size_t j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
+        size_t j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
 #else
-        size_t j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
+        size_t j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
 #endif
         if (j == size_t(-1))
             __throw_runtime_error("locale not supported");
         wbe = wbuf + j;
         __weeks_[i].assign(wbuf, wbe);
-        strftime_l(buf, 100, "%a", &t, __loc_);
+        strftime_l(buf, countof(buf), "%a", &t, __loc_);
         mb = mbstate_t();
         bb = buf;
 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
-        j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
+        j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
 #else
-        j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
+        j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
 #endif
         if (j == size_t(-1))
             __throw_runtime_error("locale not supported");
@@ -4959,25 +4971,25 @@
     for (int i = 0; i < 12; ++i)
     {
         t.tm_mon = i;
-        strftime_l(buf, 100, "%B", &t, __loc_);
+        strftime_l(buf, countof(buf), "%B", &t, __loc_);
         mb = mbstate_t();
         const char* bb = buf;
 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
-        size_t j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
+        size_t j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
 #else
-        size_t j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
+        size_t j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
 #endif
         if (j == size_t(-1))
             __throw_runtime_error("locale not supported");
         wbe = wbuf + j;
         __months_[i].assign(wbuf, wbe);
-        strftime_l(buf, 100, "%b", &t, __loc_);
+        strftime_l(buf, countof(buf), "%b", &t, __loc_);
         mb = mbstate_t();
         bb = buf;
 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
-        j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
+        j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
 #else
-        j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
+        j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
 #endif
         if (j == size_t(-1))
             __throw_runtime_error("locale not supported");
@@ -4986,26 +4998,26 @@
     }
     // __am_pm_
     t.tm_hour = 1;
-    strftime_l(buf, 100, "%p", &t, __loc_);
+    strftime_l(buf, countof(buf), "%p", &t, __loc_);
     mb = mbstate_t();
     const char* bb = buf;
 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
-    size_t j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
+    size_t j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
 #else
-    size_t j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
+    size_t j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
 #endif
     if (j == size_t(-1))
         __throw_runtime_error("locale not supported");
     wbe = wbuf + j;
     __am_pm_[0].assign(wbuf, wbe);
     t.tm_hour = 13;
-    strftime_l(buf, 100, "%p", &t, __loc_);
+    strftime_l(buf, countof(buf), "%p", &t, __loc_);
     mb = mbstate_t();
     bb = buf;
 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
-    j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
+    j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
 #else
-    j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, __loc_);
+    j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, __loc_);
 #endif
     if (j == size_t(-1))
         __throw_runtime_error("locale not supported");
@@ -5268,7 +5280,7 @@
     char fmt[] = {'%', __fmt, __mod, 0};
     if (__mod != 0)
         swap(fmt[1], fmt[2]);
-    size_t n = strftime_l(__nb, static_cast<size_t>(__ne-__nb), fmt, __tm, __loc_);
+    size_t n = strftime_l(__nb, countof(__nb, __ne), fmt, __tm, __loc_);
     __ne = __nb + n;
 }
 
@@ -5282,9 +5294,9 @@
     mbstate_t mb = {0,{0}};
     const char* __nb = __nar;
 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
-    size_t j = mbsrtowcs_l(__wb, &__nb, 100, &mb, __loc_);
+    size_t j = mbsrtowcs_l(__wb, &__nb, countof(__wb, __we), &mb, __loc_);
 #else
-    size_t j = __mbsrtowcs_l(__wb, &__nb, 100, &mb, __loc_);
+    size_t j = __mbsrtowcs_l(__wb, &__nb, countof(__wb, __we), &mb, __loc_);
 #endif
     if (j == size_t(-1))
         __throw_runtime_error("locale not supported");
@@ -5807,9 +5819,9 @@
     mbstate_t mb = {0,{0}};
     const char* bb = lc->currency_symbol;
 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
-    size_t j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get());
+    size_t j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
 #else
-    size_t j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get());
+    size_t j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
 #endif
     if (j == size_t(-1))
         __throw_runtime_error("locale not supported");
@@ -5826,9 +5838,9 @@
         mb = mbstate_t();
         bb = lc->positive_sign;
 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
-        j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get());
+        j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
 #else
-        j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get());
+        j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
 #endif
         if (j == size_t(-1))
             __throw_runtime_error("locale not supported");
@@ -5842,9 +5854,9 @@
         mb = mbstate_t();
         bb = lc->negative_sign;
 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
-        j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get());
+        j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
 #else
-        j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get());
+        j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
 #endif
         if (j == size_t(-1))
             __throw_runtime_error("locale not supported");
@@ -5890,9 +5902,9 @@
     mbstate_t mb = {0,{0}};
     const char* bb = lc->int_curr_symbol;
 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
-    size_t j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get());
+    size_t j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
 #else
-    size_t j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get());
+    size_t j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
 #endif
     if (j == size_t(-1))
         __throw_runtime_error("locale not supported");
@@ -5913,9 +5925,9 @@
         mb = mbstate_t();
         bb = lc->positive_sign;
 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
-        j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get());
+        j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
 #else
-        j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get());
+        j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
 #endif
         if (j == size_t(-1))
             __throw_runtime_error("locale not supported");
@@ -5933,9 +5945,9 @@
         mb = mbstate_t();
         bb = lc->negative_sign;
 #ifdef _LIBCPP_LOCALE__L_EXTENSIONS
-        j = mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get());
+        j = mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
 #else
-        j = __mbsrtowcs_l(wbuf, &bb, sizeof(wbuf)/sizeof(wbuf[0]), &mb, loc.get());
+        j = __mbsrtowcs_l(wbuf, &bb, countof(wbuf), &mb, loc.get());
 #endif
         if (j == size_t(-1))
             __throw_runtime_error("locale not supported");
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to