String library.

Dependents:   CheckSum RN41 RealTimeClock TVZ_MU_Seminar ... more

Committer:
AkinoriHashimoto
Date:
Wed Sep 02 04:47:08 2015 +0000
Revision:
4:17e03f0747d9
Parent:
2:14f3ff21096e
Child:
5:9afdff23f890
add strCompareComplete().

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AkinoriHashimoto 0:058192932ad2 1 #include "StrLib.h"
AkinoriHashimoto 0:058192932ad2 2
AkinoriHashimoto 0:058192932ad2 3 long A2I(string str, int base) // Error; rtn '-1'
AkinoriHashimoto 0:058192932ad2 4 {
AkinoriHashimoto 0:058192932ad2 5 long ans= 0, tmp= 0;
AkinoriHashimoto 0:058192932ad2 6 int leng= str.size(); // String Length;
AkinoriHashimoto 0:058192932ad2 7 char chr[1];
AkinoriHashimoto 0:058192932ad2 8 for(int i=0; i<leng; i++) {
AkinoriHashimoto 0:058192932ad2 9 // string -> char
AkinoriHashimoto 0:058192932ad2 10 strcpy(chr, str.substr((leng-1)-i, 1).c_str());
AkinoriHashimoto 0:058192932ad2 11
AkinoriHashimoto 0:058192932ad2 12 if('0'<=chr[0] && '9'>=chr[0])
AkinoriHashimoto 0:058192932ad2 13 tmp= chr[0]-'0';
AkinoriHashimoto 0:058192932ad2 14 else if('A'<=chr[0] && 'F'>=chr[0])
AkinoriHashimoto 0:058192932ad2 15 tmp= chr[0]-'A'+0x0A; // ex. C=12= 2(C-A)+ 10(0x0A)
AkinoriHashimoto 0:058192932ad2 16 else if('a'<=chr[0] && 'f'>=chr[0])
AkinoriHashimoto 0:058192932ad2 17 tmp= chr[0]-'a'+0x0A;
AkinoriHashimoto 0:058192932ad2 18 else
AkinoriHashimoto 0:058192932ad2 19 return -1;
AkinoriHashimoto 0:058192932ad2 20 // UART_BT.printf("%s;%d\n", chr[0], tmp);
AkinoriHashimoto 0:058192932ad2 21 ans+= tmp* (int)pow((double)base,(double)i);
AkinoriHashimoto 0:058192932ad2 22 // UART_BT.printf("ans%d\n", ans);
AkinoriHashimoto 0:058192932ad2 23 }
AkinoriHashimoto 0:058192932ad2 24 return ans;
AkinoriHashimoto 0:058192932ad2 25 }
AkinoriHashimoto 0:058192932ad2 26 string I2A(int num, int base, int digitNum)
AkinoriHashimoto 0:058192932ad2 27 {
AkinoriHashimoto 0:058192932ad2 28 char tmpChr[33];
AkinoriHashimoto 0:058192932ad2 29 string format= "%";
AkinoriHashimoto 0:058192932ad2 30
AkinoriHashimoto 0:058192932ad2 31 // Digit Num.
AkinoriHashimoto 0:058192932ad2 32 if(digitNum > 0) { // 0以外
AkinoriHashimoto 0:058192932ad2 33 char tmp[1];
AkinoriHashimoto 0:058192932ad2 34 tmp[0]= '0'+ digitNum;
AkinoriHashimoto 0:058192932ad2 35 format += tmp[0];
AkinoriHashimoto 0:058192932ad2 36 format += ".";
AkinoriHashimoto 0:058192932ad2 37 format += tmp[0]; // +でつなげると、charの和(オーバーフロー)起こす。
AkinoriHashimoto 0:058192932ad2 38 }
AkinoriHashimoto 0:058192932ad2 39
AkinoriHashimoto 0:058192932ad2 40 if(base== 8)
AkinoriHashimoto 0:058192932ad2 41 format += "o";
AkinoriHashimoto 0:058192932ad2 42 if(base==10)
AkinoriHashimoto 0:058192932ad2 43 format += "d";
AkinoriHashimoto 0:058192932ad2 44 if(base==16)
AkinoriHashimoto 0:058192932ad2 45 format += "x";
AkinoriHashimoto 0:058192932ad2 46 // log_BTSD(format);
AkinoriHashimoto 0:058192932ad2 47
AkinoriHashimoto 0:058192932ad2 48 sprintf(tmpChr, format.c_str(), num);
AkinoriHashimoto 0:058192932ad2 49 string tmpStr= tmpChr;
AkinoriHashimoto 0:058192932ad2 50 return tmpStr;
AkinoriHashimoto 0:058192932ad2 51 }
AkinoriHashimoto 0:058192932ad2 52
AkinoriHashimoto 2:14f3ff21096e 53 string F2A(float num, int fieldWidth, int decimalPlaces)
AkinoriHashimoto 2:14f3ff21096e 54 {
AkinoriHashimoto 2:14f3ff21096e 55 if(!(0<=fieldWidth && fieldWidth<30))
AkinoriHashimoto 2:14f3ff21096e 56 return "ERR; fieldWidth.";
AkinoriHashimoto 2:14f3ff21096e 57 if(!(0<=decimalPlaces && decimalPlaces<30) )
AkinoriHashimoto 2:14f3ff21096e 58 return "ERR; decimalPlaces.";
AkinoriHashimoto 2:14f3ff21096e 59 if(fieldWidth < decimalPlaces+2)
AkinoriHashimoto 2:14f3ff21096e 60 return "ERR; fieldWidth < decimalPlaces+2";
AkinoriHashimoto 2:14f3ff21096e 61
AkinoriHashimoto 2:14f3ff21096e 62 char tmpChr[33];
AkinoriHashimoto 2:14f3ff21096e 63 string format= "%"+ I2A(fieldWidth)+ "."+ I2A(decimalPlaces)+ "f";
AkinoriHashimoto 2:14f3ff21096e 64 sprintf(tmpChr, format.c_str(), num);
AkinoriHashimoto 2:14f3ff21096e 65 string tmpStr= tmpChr;
AkinoriHashimoto 2:14f3ff21096e 66 return tmpStr;
AkinoriHashimoto 2:14f3ff21096e 67 }
AkinoriHashimoto 2:14f3ff21096e 68 string F2A(float num)
AkinoriHashimoto 2:14f3ff21096e 69 {
AkinoriHashimoto 2:14f3ff21096e 70 char tmpChr[33];
AkinoriHashimoto 2:14f3ff21096e 71 sprintf(tmpChr, "%f", num);
AkinoriHashimoto 2:14f3ff21096e 72 string tmpStr= tmpChr;
AkinoriHashimoto 2:14f3ff21096e 73 return tmpStr;
AkinoriHashimoto 2:14f3ff21096e 74 }
AkinoriHashimoto 2:14f3ff21096e 75
AkinoriHashimoto 0:058192932ad2 76 bool strCompare(string trg, string cmp, int idx)
AkinoriHashimoto 0:058192932ad2 77 {
AkinoriHashimoto 0:058192932ad2 78 int id= trg.find(cmp, idx);
AkinoriHashimoto 0:058192932ad2 79 if(id == string::npos)
AkinoriHashimoto 0:058192932ad2 80 return false;
AkinoriHashimoto 0:058192932ad2 81 return id == idx;
AkinoriHashimoto 1:7c89cd414311 82 }
AkinoriHashimoto 4:17e03f0747d9 83 bool strCompareComplete(string trg, string cmp)
AkinoriHashimoto 4:17e03f0747d9 84 {
AkinoriHashimoto 4:17e03f0747d9 85 int idx= trg.find(cmp);
AkinoriHashimoto 4:17e03f0747d9 86 if(idx == string::npos)
AkinoriHashimoto 4:17e03f0747d9 87 return false;
AkinoriHashimoto 4:17e03f0747d9 88 idx= cmp.find(trg);
AkinoriHashimoto 4:17e03f0747d9 89 if(idx == string::npos)
AkinoriHashimoto 4:17e03f0747d9 90 return false;
AkinoriHashimoto 4:17e03f0747d9 91
AkinoriHashimoto 4:17e03f0747d9 92 return true;
AkinoriHashimoto 4:17e03f0747d9 93 }
AkinoriHashimoto 1:7c89cd414311 94
AkinoriHashimoto 1:7c89cd414311 95 string toAlpanumeric(string str, bool toLarge)
AkinoriHashimoto 1:7c89cd414311 96 {
AkinoriHashimoto 1:7c89cd414311 97 string ans= "";
AkinoriHashimoto 1:7c89cd414311 98 bool num, small, large;
AkinoriHashimoto 1:7c89cd414311 99 for (int i= 0; i < str.size(); i++) {
AkinoriHashimoto 1:7c89cd414311 100 num= small= large= false;
AkinoriHashimoto 1:7c89cd414311 101 if('0' <= str[i] && str[i] <= '9')
AkinoriHashimoto 1:7c89cd414311 102 num= true;
AkinoriHashimoto 1:7c89cd414311 103 else if('A' <= str[i] && str[i] <= 'Z')
AkinoriHashimoto 1:7c89cd414311 104 large= true;
AkinoriHashimoto 1:7c89cd414311 105 else if('a' <= str[i] && str[i] <= 'z')
AkinoriHashimoto 1:7c89cd414311 106 small= true;
AkinoriHashimoto 2:14f3ff21096e 107
AkinoriHashimoto 1:7c89cd414311 108 if(toLarge && small)
AkinoriHashimoto 1:7c89cd414311 109 str[i] -= 0x20; // small -> large
AkinoriHashimoto 1:7c89cd414311 110 if(num || large || small)
AkinoriHashimoto 1:7c89cd414311 111 ans += str[i];
AkinoriHashimoto 1:7c89cd414311 112 }
AkinoriHashimoto 2:14f3ff21096e 113
AkinoriHashimoto 1:7c89cd414311 114 return ans;
AkinoriHashimoto 0:058192932ad2 115 }