{"action":"created","comment":{"url":"https://api.github.com/repos/lilydjwg/swapview/pulls/comments/22703573","id":22703573,"diff_hunk":"@@ -5,159 +5,48 @@\n #include <iomanip>\n #include <tuple>\n #include <algorithm>\n-\n-#include <cstring>\n #include <cmath>\n-#include <cctype>\n-\n #include <sys/types.h>\n #include <dirent.h>\n #include <error.h>\n \n using namespace std;\n \n-// #define TARGET \"Size:\"     // test with Size: when swap is empty\n-#define TARGET \"Swap:\"\n-#define TARGETLEN 5\n-\n-/////////////////////////////////////////////////////////////////////////////\n-// Helpers for Python to C++14 Conversion\n-\n-template<typename... UArgs>\n-class StrFormatHelper{\n-    ostringstream sout;\n-    template<typename T>\n-    void format(T value){ sout<<value; }\n-\n-    template<typename T, typename... TArgs>\n-    void format(T value, TArgs... args){\n-        sout<<value;\n-        format(args...);\n-    }\n-\n-public:\n-    StrFormatHelper(UArgs... args){ format(args...); }\n-    operator string (){ return sout.str(); }\n-};\n-\n-template<typename... UArgs>\n-string strformat(UArgs... args){\n-    return StrFormatHelper<UArgs...>(args...);\n-}\n-\n-string readall(string path){\n-    ifstream fs(path);\n-    return string((istreambuf_iterator<char>(fs)), istreambuf_iterator<char>());\n-}\n-\n-template<typename T>\n-struct range_wrapper {\n-    struct iterator {\n-        iterator(range_wrapper * rw) : rw(rw) {\n-            if(rw) ++(*this); // read the first file\n-        }\n-\n-        typename T::value_type\n-        operator * (void) const { \n-            return rw->t.get();\n-        }\n-\n-        iterator& operator ++ (void) {\n-            rw->t.next(); \n-            return *this;\n-        }\n-\n-        bool end(void) const { return (!rw) || (rw->t.end()); }\n-\n-        bool operator == (iterator const& i) const {\n-            return (end() == i.end());\n-        }\n-\n-        bool operator != (iterator const& i ) const {\n-            return (end() != i.end());\n-        }\n-        range_wrapper * rw;\n-    };\n+constexpr const char * TARGET = \"Swap:\";\n+constexpr const size_t TARGETLEN = 5;\n \n-    template<typename... Args>\n-    range_wrapper(Args... args)\n-        : t(args...)\n-    { }\n+using swap_info = tuple<int, double, string>;\n \n-    iterator begin() { return iterator(this); }\n-    iterator end() { return iterator(nullptr); }\n-\n-    T t;\n-};\n-\n-struct readlines {\n-    readlines (string const& path) : fs(path) { } \n-    string const& get(void) const { return cur_line; }\n-    void next(void) { getline(fs, cur_line); }\n-    bool end(void) const { return !fs; }\n-\n-    using value_type = string const&;\n-    ifstream fs;\n-    string cur_line;\n-};\n-\n-struct lsdir {\n-    lsdir(string const& path) {\n-        if((dp = opendir(path.c_str())) == nullptr) {\n-            error(errno, errno, \"opening %s \\n\", path.c_str());\n-        }\n-    }\n-    ~lsdir() { if(dp) closedir(dp); }\n-\n-    const char * get(void) const { return dirp->d_name; }\n-    void next(void) { dirp = readdir(dp); }\n-    bool end(void) const { return dirp == nullptr; }\n-\n-    using value_type = const char *;\n-    DIR * dp;\n-    struct dirent * dirp = nullptr;\n-};\n-\n-typedef tuple<int, double, string> swap_info;\n-\n-\n-/////////////////////////////////////////////////////////////////////////////\n-// Converted C++14 code\n-\n-string filesize(auto size){\n-    char units [] = \"KMGT\";\n-    auto left = fabs(size);\n+string filesize(double size) {\n+    static const char * units = \"KMGT\";\n+    auto left = abs(size); \n     int unit = -1;\n     while( left > 1100 && unit < 3 ){\n         left /= 1024;\n         unit++;\n     }\n-    if(unit == -1){\n-        return strformat(static_cast<int>(size), 'B');\n-    }else{\n-        if(size<0){\n-            left = -left;\n-        }\n-        return strformat(fixed, setprecision(1), left, units[unit], \"iB\");\n-    }\n+    if(unit == -1) return (to_string((int)size) + 'B');\n+\n+    ostringstream sout;\n+    sout << fixed << setprecision(1) << (size > 0 ? left : -left) << units[unit] << \"iB\";\n+    return sout.str();\n }\n \n bool starts_with (string const& s1, string const& s2) {\n-    if(s1.size() < s2.size())\n-        return false;\n-\n+    if(s1.size() < s2.size()) return false;\n     auto r = mismatch(s2.begin(), s2.end(), s1.begin());\n     return (r.first == s2.end());\n }\n \n swap_info getSwapFor(int pid){\n-    string comm = readall(strformat(\"/proc/\", pid, \"/cmdline\"));\n-    if(comm.length() > 0){\n-      replace(comm.begin(), comm.end(), '\\0' , ' ');\n-      comm.pop_back();\n-    }\n+    ifstream cmd_inf(\"/proc/\" + to_string(pid) + \"/cmdline\");\n+    string comm((istreambuf_iterator<char>(cmd_inf)), istreambuf_iterator<char>());\n+    replace(comm.begin(), comm.end(), '\\0' , ' ');\n+    if(!comm.empty()) comm.pop_back();","path":"C++14/swapview.cpp","position":170,"original_position":170,"commit_id":"0700de2dd437a57f9f802eea5351c5d44e622768","original_commit_id":"0700de2dd437a57f9f802eea5351c5d44e622768","user":{"login":"coolwanglu","id":734614,"avatar_url":"https://avatars.githubusercontent.com/u/734614?v=3","gravatar_id":"","url":"https://api.github.com/users/coolwanglu","html_url":"https://github.com/coolwanglu","followers_url":"https://api.github.com/users/coolwanglu/followers","following_url":"https://api.github.com/users/coolwanglu/following{/other_user}","gists_url":"https://api.github.com/users/coolwanglu/gists{/gist_id}","starred_url":"https://api.github.com/users/coolwanglu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/coolwanglu/subscriptions","organizations_url":"https://api.github.com/users/coolwanglu/orgs","repos_url":"https://api.github.com/users/coolwanglu/repos","events_url":"https://api.github.com/users/coolwanglu/events{/privacy}","received_events_url":"https://api.github.com/users/coolwanglu/received_events","type":"User","site_admin":false},"body":"你觉得跟我改之前逻辑一样吗\r\n2015-1-9 下午2:47于 \"依云\" <notifications@github.com>写道：\r\n\r\n> In C++14/swapview.cpp\r\n> <https://github.com/lilydjwg/swapview/pull/45#discussion-diff-22703494>:\r\n>\r\n> >      auto r = mismatch(s2.begin(), s2.end(), s1.begin());\r\n> >      return (r.first == s2.end());\r\n> >  }\r\n> >\r\n> >  swap_info getSwapFor(int pid){\r\n> > -    string comm = readall(strformat(\"/proc/\", pid, \"/cmdline\"));\r\n> > -    if(comm.length() > 0){\r\n> > -      replace(comm.begin(), comm.end(), '\\0' , ' ');\r\n> > -      comm.pop_back();\r\n> > -    }\r\n> > +    ifstream cmd_inf(\"/proc/\" + to_string(pid) + \"/cmdline\");\r\n> > +    string comm((istreambuf_iterator<char>(cmd_inf)), istreambuf_iterator<char>());\r\n> > +    replace(comm.begin(), comm.end(), '\\0' , ' ');\r\n> > +    if(!comm.empty()) comm.pop_back();\r\n>\r\n> 这里 comm 的最后一个字符可能不是 null（比如 ssh 的 master 进程）。\r\n>\r\n> —\r\n> Reply to this email directly or view it on GitHub\r\n> <https://github.com/lilydjwg/swapview/pull/45/files#r22703494>.\r\n>","created_at":"2015-01-09T06:52:38Z","updated_at":"2015-01-09T06:52:38Z","html_url":"https://github.com/lilydjwg/swapview/pull/45#discussion_r22703573","pull_request_url":"https://api.github.com/repos/lilydjwg/swapview/pulls/45","_links":{"self":{"href":"https://api.github.com/repos/lilydjwg/swapview/pulls/comments/22703573"},"html":{"href":"https://github.com/lilydjwg/swapview/pull/45#discussion_r22703573"},"pull_request":{"href":"https://api.github.com/repos/lilydjwg/swapview/pulls/45"}}},"pull_request":{"url":"https://api.github.com/repos/lilydjwg/swapview/pulls/45","id":27033539,"html_url":"https://github.com/lilydjwg/swapview/pull/45","diff_url":"https://github.com/lilydjwg/swapview/pull/45.diff","patch_url":"https://github.com/lilydjwg/swapview/pull/45.patch","issue_url":"https://api.github.com/repos/lilydjwg/swapview/issues/45","number":45,"state":"open","locked":false,"title":"shorten and optimized c++14; add c++14_boost","user":{"login":"coolwanglu","id":734614,"avatar_url":"https://avatars.githubusercontent.com/u/734614?v=3","gravatar_id":"","url":"https://api.github.com/users/coolwanglu","html_url":"https://github.com/coolwanglu","followers_url":"https://api.github.com/users/coolwanglu/followers","following_url":"https://api.github.com/users/coolwanglu/following{/other_user}","gists_url":"https://api.github.com/users/coolwanglu/gists{/gist_id}","starred_url":"https://api.github.com/users/coolwanglu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/coolwanglu/subscriptions","organizations_url":"https://api.github.com/users/coolwanglu/orgs","repos_url":"https://api.github.com/users/coolwanglu/repos","events_url":"https://api.github.com/users/coolwanglu/events{/privacy}","received_events_url":"https://api.github.com/users/coolwanglu/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-01-08T15:01:12Z","updated_at":"2015-01-09T06:52:38Z","closed_at":null,"merged_at":null,"merge_commit_sha":"945be32987847b631990741c94b758466e287572","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/lilydjwg/swapview/pulls/45/commits","review_comments_url":"https://api.github.com/repos/lilydjwg/swapview/pulls/45/comments","review_comment_url":"https://api.github.com/repos/lilydjwg/swapview/pulls/comments/{number}","comments_url":"https://api.github.com/repos/lilydjwg/swapview/issues/45/comments","statuses_url":"https://api.github.com/repos/lilydjwg/swapview/statuses/0700de2dd437a57f9f802eea5351c5d44e622768","head":{"label":"coolwanglu:master","ref":"master","sha":"0700de2dd437a57f9f802eea5351c5d44e622768","user":{"login":"coolwanglu","id":734614,"avatar_url":"https://avatars.githubusercontent.com/u/734614?v=3","gravatar_id":"","url":"https://api.github.com/users/coolwanglu","html_url":"https://github.com/coolwanglu","followers_url":"https://api.github.com/users/coolwanglu/followers","following_url":"https://api.github.com/users/coolwanglu/following{/other_user}","gists_url":"https://api.github.com/users/coolwanglu/gists{/gist_id}","starred_url":"https://api.github.com/users/coolwanglu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/coolwanglu/subscriptions","organizations_url":"https://api.github.com/users/coolwanglu/orgs","repos_url":"https://api.github.com/users/coolwanglu/repos","events_url":"https://api.github.com/users/coolwanglu/events{/privacy}","received_events_url":"https://api.github.com/users/coolwanglu/received_events","type":"User","site_admin":false},"repo":{"id":28872446,"name":"swapview","full_name":"coolwanglu/swapview","owner":{"login":"coolwanglu","id":734614,"avatar_url":"https://avatars.githubusercontent.com/u/734614?v=3","gravatar_id":"","url":"https://api.github.com/users/coolwanglu","html_url":"https://github.com/coolwanglu","followers_url":"https://api.github.com/users/coolwanglu/followers","following_url":"https://api.github.com/users/coolwanglu/following{/other_user}","gists_url":"https://api.github.com/users/coolwanglu/gists{/gist_id}","starred_url":"https://api.github.com/users/coolwanglu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/coolwanglu/subscriptions","organizations_url":"https://api.github.com/users/coolwanglu/orgs","repos_url":"https://api.github.com/users/coolwanglu/repos","events_url":"https://api.github.com/users/coolwanglu/events{/privacy}","received_events_url":"https://api.github.com/users/coolwanglu/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/coolwanglu/swapview","description":"Print swap usage per process. Implemented in various programming languages","fork":true,"url":"https://api.github.com/repos/coolwanglu/swapview","forks_url":"https://api.github.com/repos/coolwanglu/swapview/forks","keys_url":"https://api.github.com/repos/coolwanglu/swapview/keys{/key_id}","collaborators_url":"https://api.github.com/repos/coolwanglu/swapview/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/coolwanglu/swapview/teams","hooks_url":"https://api.github.com/repos/coolwanglu/swapview/hooks","issue_events_url":"https://api.github.com/repos/coolwanglu/swapview/issues/events{/number}","events_url":"https://api.github.com/repos/coolwanglu/swapview/events","assignees_url":"https://api.github.com/repos/coolwanglu/swapview/assignees{/user}","branches_url":"https://api.github.com/repos/coolwanglu/swapview/branches{/branch}","tags_url":"https://api.github.com/repos/coolwanglu/swapview/tags","blobs_url":"https://api.github.com/repos/coolwanglu/swapview/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/coolwanglu/swapview/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/coolwanglu/swapview/git/refs{/sha}","trees_url":"https://api.github.com/repos/coolwanglu/swapview/git/trees{/sha}","statuses_url":"https://api.github.com/repos/coolwanglu/swapview/statuses/{sha}","languages_url":"https://api.github.com/repos/coolwanglu/swapview/languages","stargazers_url":"https://api.github.com/repos/coolwanglu/swapview/stargazers","contributors_url":"https://api.github.com/repos/coolwanglu/swapview/contributors","subscribers_url":"https://api.github.com/repos/coolwanglu/swapview/subscribers","subscription_url":"https://api.github.com/repos/coolwanglu/swapview/subscription","commits_url":"https://api.github.com/repos/coolwanglu/swapview/commits{/sha}","git_commits_url":"https://api.github.com/repos/coolwanglu/swapview/git/commits{/sha}","comments_url":"https://api.github.com/repos/coolwanglu/swapview/comments{/number}","issue_comment_url":"https://api.github.com/repos/coolwanglu/swapview/issues/comments/{number}","contents_url":"https://api.github.com/repos/coolwanglu/swapview/contents/{+path}","compare_url":"https://api.github.com/repos/coolwanglu/swapview/compare/{base}...{head}","merges_url":"https://api.github.com/repos/coolwanglu/swapview/merges","archive_url":"https://api.github.com/repos/coolwanglu/swapview/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/coolwanglu/swapview/downloads","issues_url":"https://api.github.com/repos/coolwanglu/swapview/issues{/number}","pulls_url":"https://api.github.com/repos/coolwanglu/swapview/pulls{/number}","milestones_url":"https://api.github.com/repos/coolwanglu/swapview/milestones{/number}","notifications_url":"https://api.github.com/repos/coolwanglu/swapview/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/coolwanglu/swapview/labels{/name}","releases_url":"https://api.github.com/repos/coolwanglu/swapview/releases{/id}","created_at":"2015-01-06T16:38:00Z","updated_at":"2015-01-08T17:22:41Z","pushed_at":"2015-01-08T17:22:41Z","git_url":"git://github.com/coolwanglu/swapview.git","ssh_url":"git@github.com:coolwanglu/swapview.git","clone_url":"https://github.com/coolwanglu/swapview.git","svn_url":"https://github.com/coolwanglu/swapview","homepage":null,"size":811,"stargazers_count":0,"watchers_count":0,"language":"Rust","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"lilydjwg:master","ref":"master","sha":"df4f074f11ee89d0d3f0206a6936d49e991e0b63","user":{"login":"lilydjwg","id":440661,"avatar_url":"https://avatars.githubusercontent.com/u/440661?v=3","gravatar_id":"","url":"https://api.github.com/users/lilydjwg","html_url":"https://github.com/lilydjwg","followers_url":"https://api.github.com/users/lilydjwg/followers","following_url":"https://api.github.com/users/lilydjwg/following{/other_user}","gists_url":"https://api.github.com/users/lilydjwg/gists{/gist_id}","starred_url":"https://api.github.com/users/lilydjwg/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lilydjwg/subscriptions","organizations_url":"https://api.github.com/users/lilydjwg/orgs","repos_url":"https://api.github.com/users/lilydjwg/repos","events_url":"https://api.github.com/users/lilydjwg/events{/privacy}","received_events_url":"https://api.github.com/users/lilydjwg/received_events","type":"User","site_admin":false},"repo":{"id":28377312,"name":"swapview","full_name":"lilydjwg/swapview","owner":{"login":"lilydjwg","id":440661,"avatar_url":"https://avatars.githubusercontent.com/u/440661?v=3","gravatar_id":"","url":"https://api.github.com/users/lilydjwg","html_url":"https://github.com/lilydjwg","followers_url":"https://api.github.com/users/lilydjwg/followers","following_url":"https://api.github.com/users/lilydjwg/following{/other_user}","gists_url":"https://api.github.com/users/lilydjwg/gists{/gist_id}","starred_url":"https://api.github.com/users/lilydjwg/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lilydjwg/subscriptions","organizations_url":"https://api.github.com/users/lilydjwg/orgs","repos_url":"https://api.github.com/users/lilydjwg/repos","events_url":"https://api.github.com/users/lilydjwg/events{/privacy}","received_events_url":"https://api.github.com/users/lilydjwg/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/lilydjwg/swapview","description":"Print swap usage per process. Implemented in various programming languages","fork":false,"url":"https://api.github.com/repos/lilydjwg/swapview","forks_url":"https://api.github.com/repos/lilydjwg/swapview/forks","keys_url":"https://api.github.com/repos/lilydjwg/swapview/keys{/key_id}","collaborators_url":"https://api.github.com/repos/lilydjwg/swapview/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/lilydjwg/swapview/teams","hooks_url":"https://api.github.com/repos/lilydjwg/swapview/hooks","issue_events_url":"https://api.github.com/repos/lilydjwg/swapview/issues/events{/number}","events_url":"https://api.github.com/repos/lilydjwg/swapview/events","assignees_url":"https://api.github.com/repos/lilydjwg/swapview/assignees{/user}","branches_url":"https://api.github.com/repos/lilydjwg/swapview/branches{/branch}","tags_url":"https://api.github.com/repos/lilydjwg/swapview/tags","blobs_url":"https://api.github.com/repos/lilydjwg/swapview/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/lilydjwg/swapview/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/lilydjwg/swapview/git/refs{/sha}","trees_url":"https://api.github.com/repos/lilydjwg/swapview/git/trees{/sha}","statuses_url":"https://api.github.com/repos/lilydjwg/swapview/statuses/{sha}","languages_url":"https://api.github.com/repos/lilydjwg/swapview/languages","stargazers_url":"https://api.github.com/repos/lilydjwg/swapview/stargazers","contributors_url":"https://api.github.com/repos/lilydjwg/swapview/contributors","subscribers_url":"https://api.github.com/repos/lilydjwg/swapview/subscribers","subscription_url":"https://api.github.com/repos/lilydjwg/swapview/subscription","commits_url":"https://api.github.com/repos/lilydjwg/swapview/commits{/sha}","git_commits_url":"https://api.github.com/repos/lilydjwg/swapview/git/commits{/sha}","comments_url":"https://api.github.com/repos/lilydjwg/swapview/comments{/number}","issue_comment_url":"https://api.github.com/repos/lilydjwg/swapview/issues/comments/{number}","contents_url":"https://api.github.com/repos/lilydjwg/swapview/contents/{+path}","compare_url":"https://api.github.com/repos/lilydjwg/swapview/compare/{base}...{head}","merges_url":"https://api.github.com/repos/lilydjwg/swapview/merges","archive_url":"https://api.github.com/repos/lilydjwg/swapview/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/lilydjwg/swapview/downloads","issues_url":"https://api.github.com/repos/lilydjwg/swapview/issues{/number}","pulls_url":"https://api.github.com/repos/lilydjwg/swapview/pulls{/number}","milestones_url":"https://api.github.com/repos/lilydjwg/swapview/milestones{/number}","notifications_url":"https://api.github.com/repos/lilydjwg/swapview/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/lilydjwg/swapview/labels{/name}","releases_url":"https://api.github.com/repos/lilydjwg/swapview/releases{/id}","created_at":"2014-12-23T04:31:51Z","updated_at":"2015-01-08T17:00:17Z","pushed_at":"2015-01-08T17:00:15Z","git_url":"git://github.com/lilydjwg/swapview.git","ssh_url":"git@github.com:lilydjwg/swapview.git","clone_url":"https://github.com/lilydjwg/swapview.git","svn_url":"https://github.com/lilydjwg/swapview","homepage":null,"size":811,"stargazers_count":31,"watchers_count":31,"language":"Rust","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":22,"mirror_url":null,"open_issues_count":6,"forks":22,"open_issues":6,"watchers":31,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/lilydjwg/swapview/pulls/45"},"html":{"href":"https://github.com/lilydjwg/swapview/pull/45"},"issue":{"href":"https://api.github.com/repos/lilydjwg/swapview/issues/45"},"comments":{"href":"https://api.github.com/repos/lilydjwg/swapview/issues/45/comments"},"review_comments":{"href":"https://api.github.com/repos/lilydjwg/swapview/pulls/45/comments"},"review_comment":{"href":"https://api.github.com/repos/lilydjwg/swapview/pulls/comments/{number}"},"commits":{"href":"https://api.github.com/repos/lilydjwg/swapview/pulls/45/commits"},"statuses":{"href":"https://api.github.com/repos/lilydjwg/swapview/statuses/0700de2dd437a57f9f802eea5351c5d44e622768"}}}}