Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: mtsas mtsas thermostat_fan_demo--fan mtsas ... more
MTSText.cpp
00001 #include "MTSText.h" 00002 #include "ctype.h" 00003 #include <math.h> 00004 00005 using namespace mts; 00006 00007 std::string BASE64CODE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; 00008 00009 std::string Text::getLine(const std::string& source, const size_t& start, size_t& cursor) { 00010 char delimiters[2]; 00011 delimiters[0] = '\n'; 00012 delimiters[1] = '\r'; 00013 size_t end = source.find_first_of(delimiters, start, 2); 00014 std::string line(source.substr(start, end - start)); 00015 if (end < source.size()) { 00016 if (end < source.size() - 1) 00017 if ((source[end] == '\n' && source[end + 1] == '\r') || (source[end] == '\r' && source[end + 1] == '\n')) { 00018 //Advance an additional character in scenarios where lines end in \r\n or \n\r 00019 end++; 00020 } 00021 end++; 00022 } 00023 cursor = end; 00024 return line; 00025 } 00026 00027 std::vector<std::string> Text::split(const std::string& str, char delimiter, int limit) { 00028 return split(str, std::string(1, delimiter), limit); 00029 } 00030 00031 std::vector<std::string> Text::split(const std::string& str, const std::string& delimiter, int limit) { 00032 std::vector<std::string> result; 00033 if(str.size() == 0) { 00034 return result; 00035 } 00036 size_t start = 0; 00037 size_t end = str.find(delimiter, start); 00038 for (int i = 1; i < limit || (limit <= 0 && (end != std::string::npos)); ++i) { 00039 result.push_back(str.substr(start, end - start)); 00040 start = end + delimiter.length(); 00041 end = str.find(delimiter, start); 00042 } 00043 result.push_back(str.substr(start)); 00044 return result; 00045 } 00046 00047 std::string Text::readString(char* index, int length) 00048 { 00049 std::string result = std::string(index, length); 00050 index += length; 00051 return result; 00052 } 00053 00054 std::string Text::toUpper(const std::string str) 00055 { 00056 std::string ret = str; 00057 00058 for (unsigned int i = 0; i < ret.size(); i++) 00059 { 00060 ret[i] = toupper(ret[i]); 00061 } 00062 00063 return ret; 00064 } 00065 00066 std::string Text::float2String(double val, int precision) { 00067 char buff[100]; 00068 sprintf(buff, "%d.%d", (int)val, (int)((val - floor(val)) * (int)pow(10.0, precision))); 00069 return std::string(buff); 00070 } 00071 00072 std::string Text::bin2hexString(const std::vector<uint8_t>& data, const char* delim, bool leadingZeros, bool bytePadding) { 00073 std::string ret; 00074 uint8_t *data_arr = new uint8_t[data.size()]; 00075 00076 for (size_t i = 0; i < data.size(); i++) 00077 data_arr[i] = data[i]; 00078 00079 ret = bin2hexString(data_arr, data.size(), delim, leadingZeros, bytePadding); 00080 00081 delete[] data_arr; 00082 00083 return ret; 00084 } 00085 00086 std::string Text::bin2hexString(const uint8_t* data, const uint32_t len, const char* delim, bool leadingZeros, bool bytePadding) { 00087 std::string str; 00088 char lead[] = "0x"; 00089 char buf[5]; 00090 00091 for (uint32_t i = 0; i < len; i++) { 00092 if (leadingZeros) 00093 str.append(lead); 00094 00095 if (bytePadding) 00096 snprintf(buf, sizeof(buf), "%02x", data[i]); 00097 else 00098 snprintf(buf, sizeof(buf), "%x", data[i]); 00099 00100 str.append(buf, strlen(buf)); 00101 if (i < len - 1) 00102 str.append(delim); 00103 } 00104 00105 return str; 00106 } 00107 00108 std::string Text::bin2base64(const uint8_t* data, size_t size) { 00109 std::string out; 00110 uint8_t b; 00111 00112 for (size_t i = 0; i < size; i+=3) { 00113 b = (data[i] & 0xfc) >> 2; 00114 out.push_back(BASE64CODE[b]); 00115 b = (data[i] & 0x03) << 4; 00116 if (i+1 < size) { 00117 b |= (data[i+1] & 0xf0) >> 4; 00118 out.push_back(BASE64CODE[b]); 00119 b = (data[i + 1] & 0x0f) << 2; 00120 if (i+2 < size) { 00121 b |= (data[i+2] & 0xc0) >> 6; 00122 out.push_back(BASE64CODE[b]); 00123 b = data[i+2] & 0x3f; 00124 out.push_back(BASE64CODE[b]); 00125 } else { 00126 out.push_back(BASE64CODE[b]); 00127 out.append("="); 00128 } 00129 } else { 00130 out.push_back(BASE64CODE[b]); 00131 out.append("=="); 00132 } 00133 } 00134 00135 return out; 00136 } 00137 00138 std::string Text::bin2base64(const std::vector<uint8_t>& data) { 00139 std::string out; 00140 uint8_t b; 00141 00142 for (size_t i = 0; i < data.size(); i+=3) { 00143 b = (data[i] & 0xfc) >> 2; 00144 out.push_back(BASE64CODE[b]); 00145 b = (data[i] & 0x03) << 4; 00146 if (i+1 < data.size()) { 00147 b |= (data[i+1] & 0xf0) >> 4; 00148 out.push_back(BASE64CODE[b]); 00149 b = (data[i + 1] & 0x0f) << 2; 00150 if (i+2 < data.size()) { 00151 b |= (data[i+2] & 0xc0) >> 6; 00152 out.push_back(BASE64CODE[b]); 00153 b = data[i+2] & 0x3f; 00154 out.push_back(BASE64CODE[b]); 00155 } else { 00156 out.push_back(BASE64CODE[b]); 00157 out.append("="); 00158 } 00159 } else { 00160 out.push_back(BASE64CODE[b]); 00161 out.append("=="); 00162 } 00163 } 00164 00165 return out; 00166 } 00167 00168 bool Text::base642bin(const std::string in, std::vector<uint8_t>& out) { 00169 00170 if (in.find_first_not_of(BASE64CODE) != std::string::npos) { 00171 return false; 00172 } 00173 00174 if (in.size() % 4 != 0) { 00175 return false; 00176 } 00177 00178 uint8_t a,b,c,d; 00179 00180 for (uint32_t i = 0; i < in.size(); i+=4) { 00181 a = BASE64CODE.find(in[i]); 00182 b = BASE64CODE.find(in[i+1]); 00183 c = BASE64CODE.find(in[i+2]); 00184 d = BASE64CODE.find(in[i+3]); 00185 out.push_back(a << 2 | b >> 4); 00186 out.push_back(b << 4 | c >> 2); 00187 out.push_back(c << 6 | d); 00188 } 00189 00190 return true; 00191 } 00192 00193 00194 void Text::ltrim(std::string& str, const char* args) { 00195 size_t startpos = str.find_first_not_of(args); 00196 if (startpos != std::string::npos) 00197 str = str.substr(startpos); 00198 } 00199 00200 void Text::rtrim(std::string& str, const char* args) { 00201 size_t endpos = str.find_last_not_of(args); 00202 if (endpos != std::string::npos) 00203 str = str.substr(0, endpos + 1); 00204 } 00205 00206 void Text::trim(std::string& str, const char* args) { 00207 ltrim(str, args); 00208 rtrim(str, args); 00209 }
Generated on Thu Jul 14 2022 17:11:00 by
1.7.2