Explorar o código

optimize code

ithewei %!s(int64=4) %!d(string=hai) anos
pai
achega
fa453970c5
Modificáronse 10 ficheiros con 79 adicións e 22 borrados
  1. 8 0
      base/htime.c
  2. 3 1
      base/htime.h
  3. 16 0
      cpputil/hfile.h
  4. 11 2
      cpputil/hstring.cpp
  5. 15 2
      cpputil/hstring.h
  6. 2 2
      cpputil/iniparser.cpp
  7. 13 4
      docs/API.md
  8. 1 3
      event/hloop.c
  9. 2 0
      unittest/date_test.c
  10. 8 8
      unittest/hstring_test.cpp

+ 8 - 0
base/htime.c

@@ -158,6 +158,14 @@ char* datetime_fmt(datetime_t* dt, char* buf) {
     return buf;
 }
 
+char* datetime_fmt_iso(datetime_t* dt, char* buf) {
+    sprintf(buf, DATETIME_FMT_ISO,
+        dt->year, dt->month, dt->day,
+        dt->hour, dt->min, dt->sec,
+        dt->ms);
+    return buf;
+}
+
 char* gmtime_fmt(time_t time, char* buf) {
     struct tm* tm = gmtime(&time);
     //strftime(buf, GMTIME_FMT_BUFLEN, "%a, %d %b %Y %H:%M:%S GMT", tm);

+ 3 - 1
base/htime.h

@@ -76,8 +76,10 @@ HV_EXPORT datetime_t* datetime_future(datetime_t* dt, int days DEFAULT(1));
 HV_EXPORT char* duration_fmt(int sec, char* buf);
 
 #define DATETIME_FMT        "%04d-%02d-%02d %02d:%02d:%02d"
-#define DATETIME_FMT_BUFLEN 24
+#define DATETIME_FMT_ISO    "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ"
+#define DATETIME_FMT_BUFLEN 30
 HV_EXPORT char* datetime_fmt(datetime_t* dt, char* buf);
+HV_EXPORT char* datetime_fmt_iso(datetime_t* dt, char* buf);
 
 #define GMTIME_FMT          "%.3s, %02d %.3s %04d %02d:%02d:%02d GMT"
 #define GMTIME_FMT_BUFLEN   30

+ 16 - 0
cpputil/hfile.h

@@ -43,6 +43,22 @@ public:
         return fwrite(ptr, 1, len, fp);
     }
 
+    size_t write(const std::string& str) {
+        return write(str.c_str(), str.length());
+    }
+
+    int seek(size_t offset, int whence = SEEK_SET) {
+        return fseek(fp, offset, whence);
+    }
+
+    int tell() {
+        return ftell(fp);
+    }
+
+    int flush() {
+        return fflush(fp);
+    }
+
     static size_t size(const char* filepath) {
         struct stat st;
         memset(&st, 0, sizeof(st));

+ 11 - 2
cpputil/hstring.cpp

@@ -151,13 +151,13 @@ std::string trim(const std::string& str, const char* chars) {
     return str.substr(pos1, pos2-pos1+1);
 }
 
-std::string trimL(const std::string& str, const char* chars) {
+std::string ltrim(const std::string& str, const char* chars) {
     std::string::size_type pos = str.find_first_not_of(chars);
     if (pos == std::string::npos)    return "";
     return str.substr(pos);
 }
 
-std::string trimR(const std::string& str, const char* chars) {
+std::string rtrim(const std::string& str, const char* chars) {
     std::string::size_type pos = str.find_last_not_of(chars);
     return str.substr(0, pos+1);
 }
@@ -178,6 +178,15 @@ std::string trim_pairs(const std::string& str, const char* pairs) {
 }
 
 std::string replace(const std::string& str, const std::string& find, const std::string& rep) {
+    std::string res(str);
+    std::string::size_type pos = res.find(find);
+    if (pos != std::string::npos) {
+        res.replace(pos, find.size(), rep);
+    }
+    return res;
+}
+
+std::string replaceAll(const std::string& str, const std::string& find, const std::string& rep) {
     std::string::size_type pos = 0;
     std::string::size_type a = find.size();
     std::string::size_type b = rep.size();

+ 15 - 2
cpputil/hstring.h

@@ -3,6 +3,8 @@
 
 #include <string>
 #include <vector>
+
+#include <iostream>
 #include <sstream>
 
 #include "hexport.h"
@@ -40,6 +42,16 @@ HV_INLINE T from_string(const std::string& str) {
     return t;
 }
 
+template<typename T>
+HV_INLINE void print(const T& t) {
+    std::cout << t;
+}
+
+template<typename T>
+HV_INLINE void println(const T& t) {
+    std::cout << t << std::endl;
+}
+
 HV_EXPORT std::string& toupper(std::string& str);
 HV_EXPORT std::string& tolower(std::string& str);
 HV_EXPORT std::string& reverse(std::string& str);
@@ -54,10 +66,11 @@ HV_EXPORT StringList split(const std::string& str, char delim = ',');
 // k1=v1&k2=v2
 HV_EXPORT hv::KeyValue splitKV(const std::string& str, char kv_kv = '&', char k_v = '=');
 HV_EXPORT std::string trim(const std::string& str, const char* chars = SPACE_CHARS);
-HV_EXPORT std::string trimL(const std::string& str, const char* chars = SPACE_CHARS);
-HV_EXPORT std::string trimR(const std::string& str, const char* chars = SPACE_CHARS);
+HV_EXPORT std::string ltrim(const std::string& str, const char* chars = SPACE_CHARS);
+HV_EXPORT std::string rtrim(const std::string& str, const char* chars = SPACE_CHARS);
 HV_EXPORT std::string trim_pairs(const std::string& str, const char* pairs = PAIR_CHARS);
 HV_EXPORT std::string replace(const std::string& str, const std::string& find, const std::string& rep);
+HV_EXPORT std::string replaceAll(const std::string& str, const std::string& find, const std::string& rep);
 
 } // end namespace hv
 

+ 2 - 2
cpputil/iniparser.cpp

@@ -141,7 +141,7 @@ int IniParser::LoadFromMem(const char* data) {
     while (std::getline(ss, strLine)) {
         ++line;
 
-        content = trimL(strLine);
+        content = ltrim(strLine);
         if (content.length() == 0)  {
             // blank line
             strDiv += '\n';
@@ -156,7 +156,7 @@ int IniParser::LoadFromMem(const char* data) {
             content = content.substr(0, pos);
         }
 
-        content = trimR(content);
+        content = rtrim(content);
         if (content.length() == 0) {
             strDiv += strLine;
             strDiv += '\n';

+ 13 - 4
docs/API.md

@@ -117,6 +117,7 @@
 - hv_exists
 - hv_isdir
 - hv_isfile
+- hv_islink
 - getboolean
 - get_executable_path
 - get_executable_dir
@@ -252,16 +253,23 @@
 - class HRingBuf
 
 ### hstring.h
+- to_string
+- from_string
+- toupper
+- tolower
+- reverse
+- startswith
+- endswith
+- contains
 - asprintf
 - trim
-- trimL
-- trimR
+- ltrim
+- rtrim
 - trim_pairs
 - split
 - splitKV
 - replace
-- hv::to_string
-- hv::from_string
+- replaceAll
 
 ### hfile.h
 - class HFile
@@ -270,6 +278,7 @@
 - exists
 - isdir
 - isfile
+- islink
 - basename
 - dirname
 - filename

+ 1 - 3
event/hloop.c

@@ -208,8 +208,6 @@ unlock:
 }
 
 void hloop_post_event(hloop_t* loop, hevent_t* ev) {
-    char buf = '1';
-
     if (loop->sockpair[0] == -1 || loop->sockpair[1] == -1) {
         hlogw("socketpair not created!");
         return;
@@ -226,7 +224,7 @@ void hloop_post_event(hloop_t* loop, hevent_t* ev) {
     }
 
     hmutex_lock(&loop->custom_events_mutex);
-    hwrite(loop, loop->sockpair[SOCKPAIR_WRITE_INDEX], &buf, 1, NULL);
+    hwrite(loop, loop->sockpair[SOCKPAIR_WRITE_INDEX], "e", 1, NULL);
     event_queue_push_back(&loop->custom_events, ev);
     hmutex_unlock(&loop->custom_events_mutex);
 }

+ 2 - 0
unittest/date_test.c

@@ -5,6 +5,8 @@ int main(int argc, char* argv[]) {
     char buf1[DATETIME_FMT_BUFLEN];
     datetime_fmt(&dt, buf1);
     puts(buf1);
+    datetime_fmt_iso(&dt, buf1);
+    puts(buf1);
 
     time_t ts = datetime_mktime(&dt);
     char buf2[GMTIME_FMT_BUFLEN];

+ 8 - 8
unittest/hstring_test.cpp

@@ -4,10 +4,10 @@ using namespace hv;
 int main(int argc, char** argv) {
     std::string str1 = "a1B2*C3d4==";
     std::string str2 = "a1B2*C3d4==";
-    printf("toupper %s\n", toupper(str1).c_str());
-    printf("tolower %s\n", tolower(str2).c_str());
+    println("toupper=" + toupper(str1));
+    println("tolower=" + tolower(str2));
     std::string str3 = "abcdefg";
-    printf("reverse %s\n", reverse(str3).c_str());
+    println("reverse=" + reverse(str3));
 
     std::string str4 = "123456789";
     printf("startswith=%d\nendswith=%d\ncontains=%d\n",
@@ -16,13 +16,13 @@ int main(int argc, char** argv) {
         (int)contains(str4, "456"));
 
     std::string str5 = asprintf("%s%d", "hello", 5);
-    printf("asprintf %s\n", str5.c_str());
+    println("asprintf=" + str5);
 
     std::string str6("123,456,789");
     StringList strlist = split(str6, ',');
-    printf("split %s\n", str6.c_str());
+    println("split " + str6);
     for (auto& str : strlist) {
-        printf("%s\n", str.c_str());
+        println(str);
     }
 
     std::string str7("user=admin&pswd=123456");
@@ -33,11 +33,11 @@ int main(int argc, char** argv) {
 
     std::string str8("<stdio.h>");
     std::string str9 = trim_pairs(str8);
-    printf("trim_pairs %s\n", str9.c_str());
+    println("trim_pairs=" + str9);
 
     std::string str10("<title>{{title}}</title>");
     std::string str11 = replace(str10, "{{title}}", "Home");
-    printf("replace %s\n", str11.c_str());
+    println("replace=" + str11);
 
     return 0;
 }