Skip to content

Commit

Permalink
1. add FLG_daemon in flag.cc 2. mkconf nicely 3. remove templates in …
Browse files Browse the repository at this point in the history
…fastring
  • Loading branch information
idealvin committed Nov 12, 2019
1 parent aa97cc0 commit ef1303d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 66 deletions.
71 changes: 16 additions & 55 deletions base/fastring.h
Original file line number Diff line number Diff line change
Expand Up @@ -339,16 +339,6 @@ inline fastring operator+(const char* a, const fastring& b) {
return fastring(b.size() + n + 1).append(a, n).append(b);
}

template<typename S>
inline fastring operator+(const fastring& a, const S& b) {
return fastring(a.size() + b.size() + 1).append(a).append(b);
}

template<typename S>
inline fastring operator+(const S& a, const fastring& b) {
return fastring(a.size() + b.size() + 1).append(a).append(b);
}

inline bool operator==(const fastring& a, const fastring& b) {
if (a.size() != b.size()) return false;
return a.size() == 0 || memcmp(a.data(), b.data(), a.size()) == 0;
Expand All @@ -359,14 +349,7 @@ inline bool operator==(const fastring& a, const char* b) {
return a.size() == 0 || memcmp(a.data(), b, a.size()) == 0;
}

template<typename S>
inline bool operator==(const fastring& a, const S& b) {
if (a.size() != b.size()) return false;
return a.size() == 0 || memcmp(a.data(), b.data(), a.size()) == 0;
}

template<typename S>
inline bool operator==(const S& a, const fastring& b) {
inline bool operator==(const char* a, const fastring& b) {
return b == a;
}

Expand All @@ -378,13 +361,7 @@ inline bool operator!=(const fastring& a, const char* b) {
return !(a == b);
}

template<typename S>
inline bool operator!=(const fastring& a, const S& b) {
return !(a == b);
}

template<typename S>
inline bool operator!=(const S& a, const fastring& b) {
inline bool operator!=(const char* a, const fastring& b) {
return b != a;
}

Expand All @@ -405,15 +382,6 @@ inline bool operator<(const fastring& a, const char* b) {
}
}

template<typename S>
inline bool operator<(const fastring& a, const S& b) {
if (a.size() < b.size()) {
return a.size() == 0 || memcmp(a.data(), b.data(), a.size()) <= 0;
} else {
return memcmp(a.data(), b.data(), b.size()) < 0;
}
}

inline bool operator>(const fastring& a, const fastring& b) {
if (a.size() > b.size()) {
return b.size() == 0 || memcmp(a.data(), b.data(), b.size()) >= 0;
Expand All @@ -431,42 +399,35 @@ inline bool operator>(const fastring& a, const char* b) {
}
}

template<typename S>
inline bool operator>(const fastring& a, const S& b) {
if (a.size() > b.size()) {
return b.size() == 0 || memcmp(a.data(), b.data(), b.size()) >= 0;
} else {
return memcmp(a.data(), b.data(), a.size()) > 0;
}
}

template<typename S>
inline bool operator<(const S& a, const fastring& b) {
inline bool operator<(const char* a, const fastring& b) {
return b > a;
}

template<typename S>
inline bool operator>(const S& a, const fastring& b) {
inline bool operator>(const char* a, const fastring& b) {
return b < a;
}

template<typename S>
inline bool operator<=(const fastring& a, const S& b) {
inline bool operator<=(const fastring& a, const fastring& b) {
return !(a > b);
}

inline bool operator<=(const fastring& a, const char* b) {
return !(a > b);
}

template<typename S>
inline bool operator<=(const S& a, const fastring& b) {
inline bool operator<=(const char* a, const fastring& b) {
return !(b < a);
}

template<typename S>
inline bool operator>=(const fastring& a, const S& b) {
inline bool operator>=(const fastring& a, const fastring& b) {
return !(a < b);
}

inline bool operator>=(const fastring& a, const char* b) {
return !(a < b);
}

template<typename S>
inline bool operator>=(const S& a, const fastring& b) {
inline bool operator>=(const char* a, const fastring& b) {
return !(b > a);
}

Expand Down
17 changes: 11 additions & 6 deletions base/flag.cc
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#include "flag.h"
#include "log.h"
#include "fs.h"
#include "os.h"
#include "str.h"

#include <stdlib.h>
#include <map>

DEF_string(config, "", ".path of config file");
DEF_bool(mkconf, false, ".generate config file");
DEF_bool(daemon, false, "run program as a daemon");

namespace flag {
namespace xx {
Expand Down Expand Up @@ -191,7 +193,7 @@ void mkconf(const fastring& exe) {
flag_groups[fastring(it->second.file)].insert(*it);
}

fastring fname(exe);
fastring fname(exe.clone());
if (fname.ends_with(".exe")) fname.resize(fname.size() - 4);
fname += ".conf";

Expand All @@ -201,11 +203,13 @@ void mkconf(const fastring& exe) {
return;
}

size_t p = fname.rfind('\\');
if (p == fname.npos) p = fname.rfind('/');
fastring config_name = (p != fname.npos ? fname.substr(p + 1) : fname);
f << "### " << config_name << '\n'
<< "### # or // for comments\n";
size_t p = exe.rfind('\\');
if (p == exe.npos) p = exe.rfind('/');
fastring exe_name = (p != exe.npos ? exe.substr(p + 1) : exe);
f << fastring(49, '#') << '\n'
<< "### config for " << exe_name << '\n'
<< "### # or // for comments\n"
<< fastring(49, '#') << "\n\n";

for (auto it = flag_groups.begin(); it != flag_groups.end(); ++it) {
const auto& flags = it->second;
Expand Down Expand Up @@ -430,6 +434,7 @@ std::vector<fastring> init(int argc, char** argv) {
xx::parse_config(FLG_config);
}

if (FLG_daemon) os::daemon();
return v;
}

Expand Down
4 changes: 2 additions & 2 deletions test/rpc/hello_world.proto
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ hello_world.req {

hello_world.res {
"method": "hello_world",
"err", 200,
"errmsg", "200 ok"
"err": 200,
"errmsg": "200 ok"
}
22 changes: 19 additions & 3 deletions unitest/base/fastring_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,36 @@ DEF_test(fastring) {
DEF_case(cmp) {
fastring s = "88888888";
EXPECT_EQ(s.size(), 8);
EXPECT_EQ("88888888", s);
EXPECT_EQ(s, "88888888");
EXPECT_EQ(s, fastring("88888888"));

EXPECT_NE("8888888", s);
EXPECT_NE(s, "8888888");
EXPECT_NE(s, fastring("8888888"));
EXPECT_NE(s, "888888888");
EXPECT_NE(s, "xxxxxx");

EXPECT_GT(s, "7777777")
EXPECT_GT(s, "77777777")
EXPECT_GT(s, "777777777")
EXPECT_LT("7777777", s);
EXPECT_GT(s, "7777777");
EXPECT_GT(s, fastring("7777777"));
EXPECT_GT(s, "77777777");
EXPECT_GT(s, "777777777");

EXPECT_GT("9999999", s);
EXPECT_LT(s, "9999999");
EXPECT_LT(s, fastring("9999999"));
EXPECT_LT(s, "99999999");
EXPECT_LT(s, "999999999");

EXPECT_LE("88888888", s);
EXPECT_GE(s, "88888888");
EXPECT_GE(s, fastring("88888888"));
EXPECT_GE(s, "777777777")

EXPECT_GE("88888888", s);
EXPECT_LE(s, "88888888");
EXPECT_LE(s, fastring("88888888"));
EXPECT_LE(s, "9999999");
}

Expand Down

0 comments on commit ef1303d

Please sign in to comment.