]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
common/util: remove unit_to_bytesize, use strict_iecstrtoll instead
authorJan Fajerski <jfajerski@suse.com>
Tue, 30 Jan 2018 08:48:05 +0000 (09:48 +0100)
committerJan Fajerski <jfajerski@suse.com>
Tue, 3 Apr 2018 07:23:24 +0000 (09:23 +0200)
Signed-off-by: Jan Fajerski <jfajerski@suse.com>
src/common/util.cc
src/include/util.h
src/test/common/test_util.cc

index 51bd8b17ea9b220bf721734c59222fb4d3f332c2..6ba09be038635ef40cf5ccaf7255eec6494fc618 100644 (file)
 
 #include <stdio.h>
 
-int64_t unit_to_bytesize(string val, ostream *pss)
-{
-  if (val.empty()) {
-    if (pss)
-      *pss << "value is empty!";
-    return -EINVAL;
-  }
-
-  char c = val[val.length()-1];
-  int modifier = 0;
-  if (!::isdigit(c)) {
-    if (val.length() < 2) {
-      if (pss)
-        *pss << "invalid value: " << val;
-      return -EINVAL;
-    }
-    val = val.substr(0,val.length()-1);
-    switch (c) {
-    case 'B':
-      break;
-    case 'k':
-    case 'K':
-      modifier = 10;
-      break;
-    case 'M':
-      modifier = 20;
-      break;
-    case 'G':
-      modifier = 30;
-      break;
-    case 'T':
-      modifier = 40;
-      break;
-    case 'P':
-      modifier = 50;
-      break;
-    case 'E':
-      modifier = 60;
-      break;
-    default:
-      if (pss)
-        *pss << "unrecognized modifier '" << c << "'" << std::endl;
-      return -EINVAL;
-    }
-  }
-
-  if (val[0] == '+' || val[0] == '-') {
-    if (pss)
-      *pss << "expected numerical value, got: " << val;
-    return -EINVAL;
-  }
-
-  string err;
-  int64_t r = strict_strtoll(val.c_str(), 10, &err);
-  if ((r == 0) && !err.empty()) {
-    if (pss)
-      *pss << err;
-    return -1;
-  }
-  if (r < 0) {
-    if (pss)
-      *pss << "unable to parse positive integer '" << val << "'";
-    return -1;
-  }
-  return (r * (1LL << modifier));
-}
-
 int get_fs_stats(ceph_data_stats_t &stats, const char *path)
 {
   if (!path)
index 03ba57621290389bb938c2c40ed1a70dcf9bb359..88eab27ad322da3a5484449f1700c4f003aad313 100644 (file)
@@ -17,8 +17,6 @@
 #include "common/Formatter.h"
 #include "include/types.h"
 
-int64_t unit_to_bytesize(string val, ostream *pss);
-
 std::string bytes2str(uint64_t count);
 
 struct ceph_data_stats
index 64eace923c20b93ca980e3b0124996f39c7760d7..b47bec688e769c41b4a9abbd5182435f8be2f5c9 100644 (file)
 
 #include <sstream>
 
-TEST(util, unit_to_bytesize)
-{
-  ASSERT_EQ(1234ll, unit_to_bytesize("1234", &cerr));
-  ASSERT_EQ(1024ll, unit_to_bytesize("1K", &cerr));
-  ASSERT_EQ(1024ll, unit_to_bytesize("1k", &cerr));
-  ASSERT_EQ(1048576ll, unit_to_bytesize("1M", &cerr));
-  ASSERT_EQ(1073741824ll, unit_to_bytesize("1G", &cerr));
-  ASSERT_EQ(1099511627776ll, unit_to_bytesize("1T", &cerr));
-  ASSERT_EQ(1125899906842624ll, unit_to_bytesize("1P", &cerr));
-  ASSERT_EQ(1152921504606846976ll, unit_to_bytesize("1E", &cerr));
-
-  ASSERT_EQ(65536ll, unit_to_bytesize(" 64K", &cerr));
-}
-
 #if defined(__linux__)
 TEST(util, collect_sys_info)
 {