瀏覽代碼

Accept k and m as well as K and M in util::getRealSize()

Tatsuhiro Tsujikawa 12 年之前
父節點
當前提交
6a976a4118
共有 2 個文件被更改,包括 10 次插入3 次删除
  1. 8 3
      src/util.cc
  2. 2 0
      test/UtilTest.cc

+ 8 - 3
src/util.cc

@@ -1266,16 +1266,21 @@ std::string getHomeDir()
 
 int64_t getRealSize(const std::string& sizeWithUnit)
 {
-  std::string::size_type p = sizeWithUnit.find_first_of("KM");
+  std::string::size_type p = sizeWithUnit.find_first_of("KMkm");
   std::string size;
   int32_t mult = 1;
   if(p == std::string::npos) {
     size = sizeWithUnit;
   } else {
-    if(sizeWithUnit[p] == 'K') {
+    switch(sizeWithUnit[p]) {
+    case 'K':
+    case 'k':
       mult = 1024;
-    } else if(sizeWithUnit[p] == 'M') {
+      break;
+    case 'M':
+    case 'm':
       mult = 1024*1024;
+      break;
     }
     size.assign(sizeWithUnit.begin(), sizeWithUnit.begin()+p);
   }

+ 2 - 0
test/UtilTest.cc

@@ -1516,6 +1516,8 @@ void UtilTest::testGetRealSize()
 {
   CPPUNIT_ASSERT_EQUAL((int64_t)4294967296LL, util::getRealSize("4096M"));
   CPPUNIT_ASSERT_EQUAL((int64_t)1024, util::getRealSize("1K"));
+  CPPUNIT_ASSERT_EQUAL((int64_t)4294967296LL, util::getRealSize("4096m"));
+  CPPUNIT_ASSERT_EQUAL((int64_t)1024, util::getRealSize("1k"));
   try {
     util::getRealSize("");
     CPPUNIT_FAIL("exception must be thrown.");