| 191 | } |
| 192 | |
| 193 | bool RedisRespParser::parseRespArray(const QByteArray &data, QList<RespType> &vResult, int &iResult) { |
| 194 | if(data.isEmpty()) |
| 195 | return false; |
| 196 | |
| 197 | if (data.at(0) == '*') { |
| 198 | RespType respType; |
| 199 | int ilen = -1; |
| 200 | int index = data.indexOf("\r\n"); |
| 201 | iResult = data.mid(1, index - 1).toInt(); // 列表中元素个数 |
| 202 | int pos = index + 2; // 第一个元素索引 |
| 203 | |
| 204 | for (int i = 0; i < iResult; i++) |
| 205 | { |
| 206 | respType.init(); |
| 207 | if(data[pos] == '$') { |
| 208 | index = data.indexOf("\r\n", pos); |
| 209 | ilen = data.mid(pos + 1, index - pos - 1).toInt(); |
| 210 | respType._formatType = '$'; |
| 211 | respType._formatLength = ilen; |
| 212 | if (ilen == -1) { |
| 213 | respType._stringValue = "nil"; |
| 214 | vResult << respType; |
| 215 | pos = index + 2; // 下一个元素索引 |
| 216 | } else if (ilen == 0) { |
| 217 | respType._stringValue = ""; |
| 218 | vResult << respType; |
| 219 | pos = index + 4; // 下一个元素索引 |
| 220 | } else { |
| 221 | respType._stringValue = data.mid(index + 2, ilen); |
| 222 | vResult << respType; // 提取并追加元素 |
| 223 | pos = index + 2 + ilen + 2; //下一个元素索引 |
| 224 | } |
| 225 | } else if(data[pos] == ':') { |
| 226 | index = data.indexOf("\r\n", pos); |
| 227 | respType._formatType = ':'; |
| 228 | respType._integerValue = data.mid(pos + 1, index - pos - 1).toLongLong(); |
| 229 | vResult << respType; |
| 230 | pos = index + 2; |
| 231 | } else if(data[pos] == '+') { |
| 232 | index = data.indexOf("\r\n", pos); |
| 233 | respType._formatType = '+'; |
| 234 | respType._stringValue = data.mid(pos + 1, index - pos - 1); |
| 235 | vResult << respType; |
| 236 | pos = index + 2; |
| 237 | } else if(data[pos] == '-') { |
| 238 | index = data.indexOf("\r\n", pos); |
| 239 | respType._formatType = '-'; |
| 240 | respType._stringValue = data.mid(pos + 1, index - pos - 1); |
| 241 | vResult << respType; |
| 242 | pos = index + 2; |
| 243 | } else if(data[pos] == '*') { |
| 244 | respType._formatType = '*'; |
| 245 | parseRespArray(data.mid(pos),respType._arrayValue,respType._arrayLength); |
| 246 | vResult << respType; |
| 247 | int iArrayLength = 0; |
| 248 | parseRespArrayLength(data.mid(pos),iArrayLength); |
| 249 | pos = pos + iArrayLength; |
| 250 | } |