From 7894bcece75a6f46065b48d82e8ad79c1f3126e3 Mon Sep 17 00:00:00 2001 From: QinZuoyan Date: Tue, 10 Jul 2018 18:41:53 +0800 Subject: [PATCH] *: use buf2int/buf2bool in string_conv.h instead of which in pegasus_utils.h (#114) Former-commit-id: fe0a6e4fc5daaf04e351845859419ad5860bd9ee [formerly f56bbda520f7754289b9723cbe11eec6ac1a3061] Former-commit-id: d46c3eb43b3dfabe5ab2b7293112cf28c4098e24 --- src/base/pegasus_utils.cpp | 43 ------------ src/base/pegasus_utils.h | 7 -- src/redis_protocol/proxy_lib/redis_parser.cpp | 2 +- .../proxy_ut/redis_proxy_test.cpp | 4 +- src/server/pegasus_write_service_impl.h | 4 +- src/shell/commands.h | 67 ++++++++++--------- 6 files changed, 41 insertions(+), 86 deletions(-) diff --git a/src/base/pegasus_utils.cpp b/src/base/pegasus_utils.cpp index 17a1141590..43d133a0e9 100644 --- a/src/base/pegasus_utils.cpp +++ b/src/base/pegasus_utils.cpp @@ -28,49 +28,6 @@ void addr2host(const ::dsn::rpc_address &addr, char *str, int len /* = 100*/) } } -bool buf2int(const char *buffer, int length, int &result) -{ - if (length <= 0) - return false; - std::string s(buffer, length); - char *p = nullptr; - long long v = ::strtoll(s.data(), &p, 10); - if (*p != 0) - return false; - if (v < INT_MIN || v > INT_MAX) // out of range - return false; - result = (int)v; - return true; -} - -bool buf2int64(const char *buffer, int length, int64_t &result) -{ - if (length <= 0) - return false; - std::string s(buffer, length); - char *p = nullptr; - long long v = ::strtoll(s.data(), &p, 10); - if (*p != 0) - return false; - if ((v == LLONG_MIN || v == LLONG_MAX) && errno == ERANGE) // out of range - return false; - result = v; - return true; -} - -bool buf2bool(const char *buffer, int length, bool &result) -{ - if (length == 4 && strncmp(buffer, "true", 4) == 0) { - result = true; - return true; - } - if (length == 5 && strncmp(buffer, "false", 5) == 0) { - result = false; - return true; - } - return false; -} - size_t c_escape_string(const char *src, size_t src_len, char *dest, size_t dest_len, bool always_escape) { diff --git a/src/base/pegasus_utils.h b/src/base/pegasus_utils.h index 87e83ac5f9..837f38c2ab 100644 --- a/src/base/pegasus_utils.h +++ b/src/base/pegasus_utils.h @@ -22,13 +22,6 @@ inline uint32_t epoch_now() { return time(nullptr) - epoch_begin; } // extract "host" from rpc_address void addr2host(const ::dsn::rpc_address &addr, char *str, int len); -// parse int from string -bool buf2int(const char *buffer, int length, int &result); -bool buf2int64(const char *buffer, int length, int64_t &result); - -// parse bool from string, must be "true" or "false". -bool buf2bool(const char *buffer, int length, bool &result); - // three-way comparison, returns value: // < 0 iff "a" < "b", // == 0 iff "a" == "b", diff --git a/src/redis_protocol/proxy_lib/redis_parser.cpp b/src/redis_protocol/proxy_lib/redis_parser.cpp index 9c54038cd6..fd280665aa 100644 --- a/src/redis_protocol/proxy_lib/redis_parser.cpp +++ b/src/redis_protocol/proxy_lib/redis_parser.cpp @@ -442,7 +442,7 @@ void redis_parser::setex(message_entry &entry) redis_simple_string result; ::dsn::blob &ttl_blob = redis_req.buffers[2].data; int ttl_seconds; - if (!pegasus::utils::buf2int(ttl_blob.data(), ttl_blob.length(), ttl_seconds)) { + if (!dsn::buf2int32(ttl_blob, ttl_seconds)) { result.is_error = true; result.message = "ERR value is not an integer or out of range"; reply_message(entry, result); diff --git a/src/redis_protocol/proxy_ut/redis_proxy_test.cpp b/src/redis_protocol/proxy_ut/redis_proxy_test.cpp index 710690b7ba..b8d812afc8 100644 --- a/src/redis_protocol/proxy_ut/redis_proxy_test.cpp +++ b/src/redis_protocol/proxy_ut/redis_proxy_test.cpp @@ -7,6 +7,8 @@ #include #include +#include + #include #include #include @@ -312,7 +314,7 @@ TEST(proxy, utils) for (int i = 0; int_buffers[i]; ++i) { int result; - bool succeed = pegasus::utils::buf2int(int_buffers[i], strlen(int_buffers[i]), result); + bool succeed = dsn::buf2int32(int_buffers[i], result); ASSERT_EQ(call_results[i].succeed, succeed); if (result == true) { diff --git a/src/server/pegasus_write_service_impl.h b/src/server/pegasus_write_service_impl.h index 4f353e5bbd..eeb3ba0bed 100644 --- a/src/server/pegasus_write_service_impl.h +++ b/src/server/pegasus_write_service_impl.h @@ -10,6 +10,8 @@ #include "base/pegasus_key_schema.h" +#include + namespace pegasus { namespace server { @@ -137,7 +139,7 @@ class pegasus_write_service::impl : public dsn::replication::replica_base new_value = update.increment; } else { int64_t old_value_int; - if (!utils::buf2int64(old_value.data(), old_value.length(), old_value_int)) { + if (!dsn::buf2int64(old_value, old_value_int)) { // invalid old value derror_replica("incr failed: decree = {}, error = {}", decree, diff --git a/src/shell/commands.h b/src/shell/commands.h index 3ad76d7358..c6194c0c91 100644 --- a/src/shell/commands.h +++ b/src/shell/commands.h @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -257,13 +258,13 @@ inline bool create_app(command_executor *e, shell_context *sc, arguments args) break; switch (c) { case 'p': - if (!::pegasus::utils::buf2int(optarg, strlen(optarg), pc)) { + if (!dsn::buf2int32(optarg, pc)) { fprintf(stderr, "parse %s as partition_count failed\n", optarg); return false; } break; case 'r': - if (!::pegasus::utils::buf2int(optarg, strlen(optarg), rc)) { + if (!dsn::buf2int32(optarg, rc)) { fprintf(stderr, "parse %s as replica_count failed\n", optarg); return false; } @@ -309,7 +310,7 @@ inline bool drop_app(command_executor *e, shell_context *sc, arguments args) break; switch (c) { case 'r': - if (!::pegasus::utils::buf2int(optarg, strlen(optarg), reserve_seconds)) { + if (!dsn::buf2int32(optarg, reserve_seconds)) { fprintf(stderr, "parse %s as reserve_seconds failed\n", optarg); return false; } @@ -335,7 +336,7 @@ inline bool recall_app(command_executor *e, shell_context *sc, arguments args) int id; std::string new_name = ""; - if (!::pegasus::utils::buf2int(args.argv[1], strlen(args.argv[1]), id)) { + if (!dsn::buf2int32(args.argv[1], id)) { fprintf(stderr, "ERROR: parse %s as id failed\n", args.argv[1]); return false; } @@ -590,7 +591,7 @@ inline bool process_timeout(command_executor *e, shell_context *sc, arguments ar return true; } else if (args.argc == 2) { int timeout; - if (!::pegasus::utils::buf2int(args.argv[1], strlen(args.argv[1]), timeout)) { + if (!dsn::buf2int32(args.argv[1], timeout)) { fprintf(stderr, "ERROR: parse %s as timeout failed\n", args.argv[1]); return false; } @@ -776,13 +777,13 @@ inline bool multi_get_range(command_executor *e, shell_context *sc, arguments ar break; switch (c) { case 'a': - if (!::pegasus::utils::buf2bool(optarg, strlen(optarg), options.start_inclusive)) { + if (!dsn::buf2bool(optarg, options.start_inclusive)) { fprintf(stderr, "invalid start_inclusive param\n"); return false; } break; case 'b': - if (!::pegasus::utils::buf2bool(optarg, strlen(optarg), options.stop_inclusive)) { + if (!dsn::buf2bool(optarg, options.stop_inclusive)) { fprintf(stderr, "invalid stop_inclusive param\n"); return false; } @@ -798,7 +799,7 @@ inline bool multi_get_range(command_executor *e, shell_context *sc, arguments ar options.sort_key_filter_pattern = unescape_str(optarg); break; case 'n': - if (!::pegasus::utils::buf2int(optarg, strlen(optarg), max_count)) { + if (!dsn::buf2int32(optarg, max_count)) { fprintf(stderr, "parse %s as max_count failed\n", optarg); return false; } @@ -966,7 +967,7 @@ inline bool set_value(command_executor *e, shell_context *sc, arguments args) std::string value = sds_to_string(args.argv[3]); int32_t ttl = 0; if (args.argc == 5) { - if (!::pegasus::utils::buf2int(args.argv[4], strlen(args.argv[4]), ttl)) { + if (!dsn::buf2int32(args.argv[4], ttl)) { fprintf(stderr, "ERROR: parse %s as ttl failed\n", args.argv[4]); return false; } @@ -1114,13 +1115,13 @@ inline bool multi_del_range(command_executor *e, shell_context *sc, arguments ar break; switch (c) { case 'a': - if (!::pegasus::utils::buf2bool(optarg, strlen(optarg), options.start_inclusive)) { + if (!dsn::buf2bool(optarg, options.start_inclusive)) { fprintf(stderr, "invalid start_inclusive param\n"); return false; } break; case 'b': - if (!::pegasus::utils::buf2bool(optarg, strlen(optarg), options.stop_inclusive)) { + if (!dsn::buf2bool(optarg, options.stop_inclusive)) { fprintf(stderr, "invalid stop_inclusive param\n"); return false; } @@ -1283,7 +1284,7 @@ inline bool incr(command_executor *e, shell_context *sc, arguments args) std::string sort_key = sds_to_string(args.argv[2]); int64_t increment = 1; if (args.argc == 4) { - if (!pegasus::utils::buf2int64(args.argv[3], strlen(args.argv[3]), increment)) { + if (!dsn::buf2int64(args.argv[3], increment)) { fprintf(stderr, "ERROR: invalid increment param\n"); return false; } @@ -1382,19 +1383,19 @@ inline bool hash_scan(command_executor *e, shell_context *sc, arguments args) detailed = true; break; case 'z': - if (!::pegasus::utils::buf2int(optarg, strlen(optarg), batch_size)) { + if (!dsn::buf2int32(optarg, batch_size)) { fprintf(stderr, "parse %s as batch_size failed\n", optarg); return false; } break; case 'n': - if (!::pegasus::utils::buf2int(optarg, strlen(optarg), max_count)) { + if (!dsn::buf2int32(optarg, max_count)) { fprintf(stderr, "parse %s as max_count failed\n", optarg); return false; } break; case 't': - if (!::pegasus::utils::buf2int(optarg, strlen(optarg), timeout_ms)) { + if (!dsn::buf2int32(optarg, timeout_ms)) { fprintf(stderr, "parse %s as timeout_ms failed\n", optarg); return false; } @@ -1407,13 +1408,13 @@ inline bool hash_scan(command_executor *e, shell_context *sc, arguments args) } break; case 'a': - if (!::pegasus::utils::buf2bool(optarg, strlen(optarg), options.start_inclusive)) { + if (!dsn::buf2bool(optarg, options.start_inclusive)) { fprintf(stderr, "invalid start_inclusive param\n"); return false; } break; case 'b': - if (!::pegasus::utils::buf2bool(optarg, strlen(optarg), options.stop_inclusive)) { + if (!dsn::buf2bool(optarg, options.stop_inclusive)) { fprintf(stderr, "invalid stop_inclusive param\n"); return false; } @@ -1565,19 +1566,19 @@ inline bool full_scan(command_executor *e, shell_context *sc, arguments args) detailed = true; break; case 'z': - if (!::pegasus::utils::buf2int(optarg, strlen(optarg), batch_size)) { + if (!dsn::buf2int32(optarg, batch_size)) { fprintf(stderr, "parse %s as batch_size failed\n", optarg); return false; } break; case 'n': - if (!::pegasus::utils::buf2int(optarg, strlen(optarg), max_count)) { + if (!dsn::buf2int32(optarg, max_count)) { fprintf(stderr, "parse %s as max_count failed\n", optarg); return false; } break; case 'p': - if (!::pegasus::utils::buf2int(optarg, strlen(optarg), partition)) { + if (!dsn::buf2int32(optarg, partition)) { fprintf(stderr, "parse %s as partition id failed\n", optarg); return false; } @@ -1587,7 +1588,7 @@ inline bool full_scan(command_executor *e, shell_context *sc, arguments args) } break; case 't': - if (!::pegasus::utils::buf2int(optarg, strlen(optarg), timeout_ms)) { + if (!dsn::buf2int32(optarg, timeout_ms)) { fprintf(stderr, "parse %s as timeout_ms failed\n", optarg); return false; } @@ -1764,19 +1765,19 @@ inline bool copy_data(command_executor *e, shell_context *sc, arguments args) target_app_name = optarg; break; case 's': - if (!::pegasus::utils::buf2int(optarg, strlen(optarg), max_split_count)) { + if (!dsn::buf2int32(optarg, max_split_count)) { fprintf(stderr, "parse %s as max_split_count failed\n", optarg); return false; } break; case 'b': - if (!::pegasus::utils::buf2int(optarg, strlen(optarg), max_batch_count)) { + if (!dsn::buf2int32(optarg, max_batch_count)) { fprintf(stderr, "parse %s as max_batch_count failed\n", optarg); return false; } break; case 't': - if (!::pegasus::utils::buf2int(optarg, strlen(optarg), timeout_ms)) { + if (!dsn::buf2int32(optarg, timeout_ms)) { fprintf(stderr, "parse %s as timeout_ms failed\n", optarg); return false; } @@ -1951,19 +1952,19 @@ inline bool clear_data(command_executor *e, shell_context *sc, arguments args) force = true; break; case 's': - if (!::pegasus::utils::buf2int(optarg, strlen(optarg), max_split_count)) { + if (!dsn::buf2int32(optarg, max_split_count)) { fprintf(stderr, "parse %s as max_split_count failed\n", optarg); return false; } break; case 'b': - if (!::pegasus::utils::buf2int(optarg, strlen(optarg), max_batch_count)) { + if (!dsn::buf2int32(optarg, max_batch_count)) { fprintf(stderr, "parse %s as max_batch_count failed\n", optarg); return false; } break; case 't': - if (!::pegasus::utils::buf2int(optarg, strlen(optarg), timeout_ms)) { + if (!dsn::buf2int32(optarg, timeout_ms)) { fprintf(stderr, "parse %s as timeout_ms failed\n", optarg); return false; } @@ -2113,19 +2114,19 @@ inline bool count_data(command_executor *e, shell_context *sc, arguments args) break; switch (c) { case 's': - if (!::pegasus::utils::buf2int(optarg, strlen(optarg), max_split_count)) { + if (!dsn::buf2int32(optarg, max_split_count)) { fprintf(stderr, "parse %s as max_split_count failed\n", optarg); return false; } break; case 'b': - if (!::pegasus::utils::buf2int(optarg, strlen(optarg), max_batch_count)) { + if (!dsn::buf2int32(optarg, max_batch_count)) { fprintf(stderr, "parse %s as max_batch_count failed\n", optarg); return false; } break; case 't': - if (!::pegasus::utils::buf2int(optarg, strlen(optarg), timeout_ms)) { + if (!dsn::buf2int32(optarg, timeout_ms)) { fprintf(stderr, "parse %s as timeout_ms failed\n", optarg); return false; } @@ -2134,13 +2135,13 @@ inline bool count_data(command_executor *e, shell_context *sc, arguments args) stat_size = true; break; case 'c': - if (!::pegasus::utils::buf2int(optarg, strlen(optarg), top_count)) { + if (!dsn::buf2int32(optarg, top_count)) { fprintf(stderr, "parse %s as top_count failed\n", optarg); return false; } break; case 'r': - if (!::pegasus::utils::buf2int(optarg, strlen(optarg), run_seconds)) { + if (!dsn::buf2int32(optarg, run_seconds)) { fprintf(stderr, "parse %s as run_seconds failed\n", optarg); return false; } @@ -2660,7 +2661,7 @@ inline bool recover(command_executor *e, shell_context *sc, arguments args) node_list_str = optarg; break; case 'w': - if (!::pegasus::utils::buf2int(optarg, strlen(optarg), wait_seconds)) { + if (!dsn::buf2int32(optarg, wait_seconds)) { fprintf(stderr, "parse %s as wait_seconds failed\n", optarg); return false; }