关键词搜索

源码搜索 ×
×

tars源码分析之7

发布2022-07-03浏览448次

详情内容

在笔试面试的时候,最常考一些字符串的问题,可千万不要忽视,如下的函数肯定很眼熟,但你真的会吗?

另外,在实际开发中,不要自己重写这些了,直接拿去用就行了:

  1. namespace tars
  2. {
  3. template <>
  4. string TC_Common::tostr<bool>(const bool &t)
  5. {
  6. char buf[2];
  7. buf[0] = t ? '1' : '0';
  8. buf[1] = '\0';
  9. return string(buf);
  10. }
  11. template <>
  12. string TC_Common::tostr<char>(const char &t)
  13. {
  14. char buf[2];
  15. snprintf(buf, 2, "%c", t);
  16. return string(buf);
  17. }
  18. template <>
  19. string TC_Common::tostr<unsigned char>(const unsigned char &t)
  20. {
  21. char buf[2];
  22. snprintf(buf, 2, "%c", t);
  23. return string(buf);
  24. }
  25. template <>
  26. string TC_Common::tostr<short>(const short &t)
  27. {
  28. char buf[16];
  29. snprintf(buf, 16, "%d", t);
  30. return string(buf);
  31. }
  32. template <>
  33. string TC_Common::tostr<unsigned short>(const unsigned short &t)
  34. {
  35. char buf[16];
  36. snprintf(buf, 16, "%u", t);
  37. return string(buf);
  38. }
  39. template <>
  40. string TC_Common::tostr<int>(const int &t)
  41. {
  42. char buf[16];
  43. snprintf(buf, 16, "%d", t);
  44. return string(buf);
  45. }
  46. template <>
  47. string TC_Common::tostr<unsigned int>(const unsigned int &t)
  48. {
  49. char buf[16];
  50. snprintf(buf, 16, "%u", t);
  51. return string(buf);
  52. }
  53. template <>
  54. string TC_Common::tostr<long>(const long &t)
  55. {
  56. char buf[32];
  57. snprintf(buf, 32, "%ld", t);
  58. return string(buf);
  59. }
  60. template <>
  61. string TC_Common::tostr<long long>(const long long &t)
  62. {
  63. char buf[32];
  64. snprintf(buf, 32, "%lld", t);
  65. return string(buf);
  66. }
  67. template <>
  68. string TC_Common::tostr<unsigned long>(const unsigned long &t)
  69. {
  70. char buf[32];
  71. snprintf(buf, 32, "%lu", t);
  72. return string(buf);
  73. }
  74. template <>
  75. string TC_Common::tostr<float>(const float &t)
  76. {
  77. char buf[32];
  78. snprintf(buf, 32, "%.5f", t);
  79. string s(buf);
  80. //去掉无效0, eg. 1.0300 -> 1.03;1.00 -> 1
  81. bool bFlag = false;
  82. int pos = int(s.size() - 1);
  83. for(; pos > 0; --pos)
  84. {
  85. if(s[pos] == '0')
  86. {
  87. bFlag = true;
  88. if(s[pos-1] == '.')
  89. {
  90. //-2为了去掉"."号
  91. pos -= 2;
  92. break;
  93. }
  94. }
  95. else
  96. {
  97. break;
  98. }
  99. }
  100. if(bFlag)
  101. s = s.substr(0, pos+1);
  102. return s;
  103. }
  104. template <>
  105. string TC_Common::tostr<double>(const double &t)
  106. {
  107. char buf[32];
  108. snprintf(buf, 32, "%.5f", t);
  109. string s(buf);
  110. //去掉无效0, eg. 1.0300 -> 1.03;1.00 -> 1
  111. bool bFlag = false;
  112. int pos = int(s.size() - 1);
  113. for(; pos > 0; --pos)
  114. {
  115. if(s[pos] == '0')
  116. {
  117. bFlag = true;
  118. if(s[pos-1] == '.')
  119. {
  120. //-2为了去掉"."号
  121. pos -= 2;
  122. break;
  123. }
  124. }
  125. else
  126. {
  127. break;
  128. }
  129. }
  130. if(bFlag)
  131. s = s.substr(0, pos+1);
  132. return s;
  133. }
  134. template <>
  135. string TC_Common::tostr<long double>(const long double &t)
  136. {
  137. char buf[32];
  138. snprintf(buf, 32, "%Lf", t);
  139. string s(buf);
  140. //去掉无效0, eg. 1.0300 -> 1.03;1.00 -> 1
  141. bool bFlag = false;
  142. int pos = int(s.size() - 1);
  143. for(; pos > 0; --pos)
  144. {
  145. if(s[pos] == '0')
  146. {
  147. bFlag = true;
  148. if(s[pos-1] == '.')
  149. {
  150. //-2为了去掉"."号
  151. pos -= 2;
  152. break;
  153. }
  154. }
  155. else
  156. {
  157. break;
  158. }
  159. }
  160. if(bFlag)
  161. s = s.substr(0, pos+1);
  162. return s;
  163. }
  164. template <>
  165. string TC_Common::tostr<std::string>(const std::string &t)
  166. {
  167. return t;
  168. }
  169. string TC_Common::trim(const string &sStr, const string &s, bool bChar)
  170. {
  171. if(sStr.empty())
  172. {
  173. return sStr;
  174. }
  175. /**
  176. * 将完全与s相同的字符串去掉
  177. */
  178. if(!bChar)
  179. {
  180. return trimright(trimleft(sStr, s, false), s, false);
  181. }
  182. return trimright(trimleft(sStr, s, true), s, true);
  183. }
  184. string TC_Common::trimleft(const string &sStr, const string &s, bool bChar)
  185. {
  186. if(sStr.empty())
  187. {
  188. return sStr;
  189. }
  190. /**
  191. * 去掉sStr左边的字符串s
  192. */
  193. if(!bChar)
  194. {
  195. if(sStr.length() < s.length())
  196. {
  197. return sStr;
  198. }
  199. if(sStr.compare(0, s.length(), s) == 0)
  200. {
  201. return sStr.substr(s.length());
  202. }
  203. return sStr;
  204. }
  205. /**
  206. * 去掉sStr左边的 字符串s中的字符
  207. */
  208. string::size_type pos = 0;
  209. while(pos < sStr.length())
  210. {
  211. if(s.find_first_of(sStr[pos]) == string::npos)
  212. {
  213. break;
  214. }
  215. pos++;
  216. }
  217. if(pos == 0) return sStr;
  218. return sStr.substr(pos);
  219. }
  220. string TC_Common::trimright(const string &sStr, const string &s, bool bChar)
  221. {
  222. if(sStr.empty())
  223. {
  224. return sStr;
  225. }
  226. /**
  227. * 去掉sStr右边的字符串s
  228. */
  229. if(!bChar)
  230. {
  231. if(sStr.length() < s.length())
  232. {
  233. return sStr;
  234. }
  235. if(sStr.compare(sStr.length() - s.length(), s.length(), s) == 0)
  236. {
  237. return sStr.substr(0, sStr.length() - s.length());
  238. }
  239. return sStr;
  240. }
  241. /**
  242. * 去掉sStr右边的 字符串s中的字符
  243. */
  244. string::size_type pos = sStr.length();
  245. while(pos != 0)
  246. {
  247. if(s.find_first_of(sStr[pos-1]) == string::npos)
  248. {
  249. break;
  250. }
  251. pos--;
  252. }
  253. if(pos == sStr.length()) return sStr;
  254. return sStr.substr(0, pos);
  255. }
  256. string TC_Common::lower(const string &s)
  257. {
  258. string sString = s;
  259. for (string::iterator iter = sString.begin(); iter != sString.end(); ++iter)
  260. {
  261. *iter = tolower(*iter);
  262. }
  263. return sString;
  264. }
  265. string TC_Common::upper(const string &s)
  266. {
  267. string sString = s;
  268. for (string::iterator iter = sString.begin(); iter != sString.end(); ++iter)
  269. {
  270. *iter = toupper(*iter);
  271. }
  272. return sString;
  273. }
  274. bool TC_Common::isdigit(const string &sInput)
  275. {
  276. string::const_iterator iter = sInput.begin();
  277. if(sInput.empty())
  278. {
  279. return false;
  280. }
  281. while(iter != sInput.end())
  282. {
  283. if (!::isdigit(*iter))
  284. {
  285. return false;
  286. }
  287. ++iter;
  288. }
  289. return true;
  290. }
  291. int TC_Common::str2tm(const string &sString, const string &sFormat, struct tm &stTm)
  292. {
  293. char *p = strptime(sString.c_str(), sFormat.c_str(), &stTm);
  294. return (p != NULL) ? 0 : -1;
  295. }
  296. int TC_Common::strgmt2tm(const string &sString, struct tm &stTm)
  297. {
  298. return str2tm(sString, "%a, %d %b %Y %H:%M:%S GMT", stTm);
  299. }
  300. string TC_Common::tm2str(const struct tm &stTm, const string &sFormat)
  301. {
  302. char sTimeString[255] = "\0";
  303. strftime(sTimeString, sizeof(sTimeString), sFormat.c_str(), &stTm);
  304. return string(sTimeString);
  305. }
  306. string TC_Common::tm2str(const time_t &t, const string &sFormat)
  307. {
  308. struct tm tt;
  309. localtime_r(&t, &tt);
  310. return tm2str(tt, sFormat);
  311. }
  312. string TC_Common::now2str(const string &sFormat)
  313. {
  314. time_t t = time(NULL);
  315. return tm2str(t, sFormat.c_str());
  316. }
  317. string TC_Common::now2GMTstr()
  318. {
  319. time_t t = time(NULL);
  320. return tm2GMTstr(t);
  321. }
  322. string TC_Common::tm2GMTstr(const time_t &t)
  323. {
  324. struct tm tt;
  325. gmtime_r(&t, &tt);
  326. return tm2str(tt, "%a, %d %b %Y %H:%M:%S GMT");
  327. }
  328. string TC_Common::tm2GMTstr(const struct tm &stTm)
  329. {
  330. return tm2str(stTm, "%a, %d %b %Y %H:%M:%S GMT");
  331. }
  332. string TC_Common::nowdate2str()
  333. {
  334. return now2str("%Y%m%d");
  335. }
  336. string TC_Common::nowtime2str()
  337. {
  338. return now2str("%H%M%S");
  339. }
  340. int64_t TC_Common::now2ms()
  341. {
  342. struct timeval tv;
  343. gettimeofday(&tv, 0);
  344. return tv.tv_sec * (int64_t)1000 + tv.tv_usec/1000;
  345. }
  346. int64_t TC_Common::now2us()
  347. {
  348. struct timeval tv;
  349. gettimeofday(&tv, 0);
  350. return tv.tv_sec * (int64_t)1000000 + tv.tv_usec;
  351. }
  352. //参照phorix的优化
  353. static char c_b2s[256][4]={"00","01","02","03","04","05","06","07","08","09","0a","0b","0c","0d","0e","0f","10","11","12","13","14","15","16","17","18","19","1a","1b","1c","1d","1e","1f","https://files.jxasp.com/image/20","https://files.jxasp.com/image/21","https://files.jxasp.com/image/22","https://files.jxasp.com/image/23","https://files.jxasp.com/image/24","https://files.jxasp.com/image/25","https://files.jxasp.com/image/26","https://files.jxasp.com/image/27","https://files.jxasp.com/image/28","https://files.jxasp.com/image/29","https://files.jxasp.com/image/2a","https://files.jxasp.com/image/2b","https://files.jxasp.com/image/2c","https://files.jxasp.com/image/2d","https://files.jxasp.com/image/2e","https://files.jxasp.com/image/2f","30","31","32","33","34","35","36","37","38","39","3a","3b","3c","3d","3e","3f","40","41","42","43","44","45","46","47","48","49","4a","4b","4c","4d","4e","4f","50","51","52","53","54","55","56","57","58","59","5a","5b","5c","5d","5e","5f","60","61","62","63","64","65","66","67","68","69","6a","6b","6c","6d","6e","6f","70","71","72","73","74","75","76","77","78","79","7a","7b","7c","7d","7e","7f","80","81","82","83","84","85","86","87","88","89","8a","8b","8c","8d","8e","8f","90","91","92","93","94","95","96","97","98","99","9a","9b","9c","9d","9e","9f","a0","a1","a2","a3","a4","a5","a6","a7","a8","a9","aa","ab","ac","ad","ae","af","b0","b1","b2","b3","b4","b5","b6","b7","b8","b9","ba","bb","bc","bd","be","bf","c0","c1","c2","c3","c4","c5","c6","c7","c8","c9","ca","cb","cc","cd","ce","cf","d0","d1","d2","d3","d4","d5","d6","d7","d8","d9","da","db","dc","dd","de","df","e0","e1","e2","e3","e4","e5","e6","e7","e8","e9","ea","eb","ec","ed","ee","ef","f0","f1","f2","f3","f4","f5","f6","f7","f8","f9","fa","fb","fc","fd","fe","ff"};
  354. string TC_Common::bin2str(const void *buf, size_t len, const string &sSep, size_t lines)
  355. {
  356. if(buf == NULL || len <=0 )
  357. {
  358. return "";
  359. }
  360. string sOut;
  361. const unsigned char *p = (const unsigned char *) buf;
  362. for (size_t i = 0; i < len; ++i, ++p)
  363. {
  364. sOut += c_b2s[*p][0];
  365. sOut += c_b2s[*p][1];
  366. sOut += sSep;
  367. //换行
  368. if ((lines != 0) && ((i+1) % lines == 0))
  369. {
  370. sOut += "\n";
  371. }
  372. }
  373. return sOut;
  374. }
  375. string TC_Common::bin2str(const string &sBinData, const string &sSep, size_t lines)
  376. {
  377. return bin2str((const void *)sBinData.data(), sBinData.length(), sSep, lines);
  378. }
  379. int TC_Common::str2bin(const char *psAsciiData, unsigned char *sBinData, int iBinSize)
  380. {
  381. int iAsciiLength = strlen(psAsciiData);
  382. int iRealLength = (iAsciiLength/2 > iBinSize)?iBinSize:(iAsciiLength/2);
  383. for (int i = 0 ; i < iRealLength ; i++)
  384. {
  385. sBinData[i] = x2c(psAsciiData + i*2);
  386. }
  387. return iRealLength;
  388. }
  389. string TC_Common::str2bin(const string &sString, const string &sSep, size_t lines)
  390. {
  391. const char *psAsciiData = sString.c_str();
  392. int iAsciiLength = sString.length();
  393. string sBinData;
  394. for (int i = 0 ; i < iAsciiLength ; i++)
  395. {
  396. sBinData += x2c(psAsciiData + i);
  397. i++;
  398. i += sSep.length(); //过滤掉分隔符
  399. if (lines != 0 && sBinData.length()%lines == 0)
  400. {
  401. i++; //过滤掉回车
  402. }
  403. }
  404. return sBinData;
  405. }
  406. char TC_Common::x2c(const string &sWhat)
  407. {
  408. register char digit;
  409. if(sWhat.length() < 2)
  410. {
  411. return '\0';
  412. }
  413. digit = (sWhat[0] >= 'A' ? ((sWhat[0] & 0xdf) - 'A')+10 : (sWhat[0] - '0'));
  414. digit *= 16;
  415. digit += (sWhat[1] >= 'A' ? ((sWhat[1] & 0xdf) - 'A')+10 : (sWhat[1] - '0'));
  416. return(digit);
  417. }
  418. string TC_Common::replace(const string &sString, const string &sSrc, const string &sDest)
  419. {
  420. if(sSrc.empty())
  421. {
  422. return sString;
  423. }
  424. string sBuf = sString;
  425. string::size_type pos = 0;
  426. while( (pos = sBuf.find(sSrc, pos)) != string::npos)
  427. {
  428. sBuf.replace(pos, sSrc.length(), sDest);
  429. pos += sDest.length();
  430. }
  431. return sBuf;
  432. }
  433. string TC_Common::replace(const string &sString, const map<string,string>& mSrcDest)
  434. {
  435. if(sString.empty())
  436. {
  437. return sString;
  438. }
  439. string tmp = sString;
  440. map<string,string>::const_iterator it = mSrcDest.begin();
  441. while(it != mSrcDest.end())
  442. {
  443. string::size_type pos = 0;
  444. while((pos = tmp.find(it->first, pos)) != string::npos)
  445. {
  446. tmp.replace(pos, it->first.length(), it->second);
  447. pos += it->second.length();
  448. }
  449. ++it;
  450. }
  451. return tmp;
  452. }
  453. bool TC_Common::matchPeriod(const string& s, const string& pat)
  454. {
  455. if(s.empty())
  456. {
  457. return false;
  458. }
  459. if(pat.empty())
  460. {
  461. return true;
  462. }
  463. if(pat.find('*') == string::npos)
  464. {
  465. return s == pat;
  466. }
  467. string::size_type sIndex = 0;
  468. string::size_type patIndex = 0;
  469. do
  470. {
  471. if(pat[patIndex] == '*')
  472. {
  473. if(s[sIndex] == '.')
  474. {
  475. return false;
  476. }
  477. while(sIndex < s.size() && s[sIndex] != '.')
  478. {
  479. ++sIndex;
  480. }
  481. patIndex++;
  482. }
  483. else
  484. {
  485. if(pat[patIndex] != s[sIndex])
  486. {
  487. return false;
  488. }
  489. ++sIndex;
  490. ++patIndex;
  491. }
  492. }
  493. while(sIndex < s.size() && patIndex < pat.size());
  494. return sIndex == s.size() && patIndex == pat.size();
  495. }
  496. bool TC_Common::matchPeriod(const string& s, const vector<string>& pat)
  497. {
  498. for(size_t i = 0; i < pat.size(); i++)
  499. {
  500. if(TC_Common::matchPeriod(s,pat[i]))
  501. {
  502. return true;
  503. }
  504. }
  505. return false;
  506. }
  507. void TC_Common::daemon()
  508. {
  509. pid_t pid;
  510. if ((pid = fork()) != 0)
  511. {
  512. exit(0);
  513. }
  514. setsid();
  515. signal(SIGINT, SIG_IGN);
  516. signal(SIGHUP, SIG_IGN);
  517. signal(SIGQUIT, SIG_IGN);
  518. signal(SIGPIPE, SIG_IGN);
  519. signal(SIGTTOU, SIG_IGN);
  520. signal(SIGTTIN, SIG_IGN);
  521. signal(SIGCHLD, SIG_IGN);
  522. signal(SIGTERM, SIG_IGN);
  523. ignorePipe();
  524. if((pid = fork()) != 0)
  525. {
  526. //父进程结束,变成daemon
  527. exit(0);
  528. }
  529. umask(0);
  530. // chdir("/");
  531. }
  532. void TC_Common::ignorePipe()
  533. {
  534. struct sigaction sig;
  535. memset(&sig,0,sizeof(struct sigaction));
  536. sig.sa_handler = SIG_IGN;
  537. sig.sa_flags = 0;
  538. sigemptyset(&sig.sa_mask);
  539. sigaction(SIGPIPE,&sig,NULL);
  540. }
  541. bool TC_Common::isPrimeNumber(size_t n)
  542. {
  543. size_t nn = (size_t)sqrt((double)n);
  544. for(size_t i = 2; i < nn;i++)
  545. {
  546. if(n % i == 0)
  547. {
  548. return false;
  549. }
  550. }
  551. return true;
  552. }
  553. size_t TC_Common::toSize(const string &s, size_t iDefaultSize)
  554. {
  555. if (s.empty())
  556. {
  557. return iDefaultSize;
  558. }
  559. char c = s[s.length()-1];
  560. if(c != 'K' && c != 'M' && c != 'G' && TC_Common::trim(s) == TC_Common::tostr(TC_Common::strto<size_t>(s)))
  561. {
  562. //没有后缀, 且转换是正确的
  563. return (size_t)(TC_Common::strto<size_t>(s));
  564. }
  565. else if(c == 'K' || c == 'M' || c == 'G')
  566. {
  567. if (s.length() == 1)
  568. {
  569. return iDefaultSize;
  570. }
  571. float n = TC_Common::strto<float>(s.substr(0, s.length()-1));
  572. if(TC_Common::trim(s.substr(0, s.length()-1)) != TC_Common::tostr(n))
  573. {
  574. return iDefaultSize;
  575. }
  576. if(c == 'K')
  577. {
  578. return (size_t)(n * 1024);
  579. }
  580. if(c == 'M')
  581. {
  582. return (size_t)(n * 1024*1024);
  583. }
  584. if(c == 'G')
  585. {
  586. return (size_t)(n * 1024 * 1024 * 1024);
  587. }
  588. }
  589. return iDefaultSize;
  590. }
  591. // Generate the randome string, a SHA1-sized random number
  592. void TC_Common::getRandomHexChars(char* p, unsigned int len)
  593. {
  594. const char* const chars = "0123456789abcdef";
  595. FILE *fp = fopen("/dev/urandom","r");
  596. if (!fp || fread(p,len,1,fp) == 0)
  597. {
  598. for (unsigned int j = 0; j < len; j++)
  599. p[j] ^= rand();
  600. }
  601. if (fp) fclose(fp);
  602. for (unsigned int j = 0; j < len; j++)
  603. p[j] = chars[p[j] & 0x0F];
  604. }
  605. }

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载