jstring.cpp 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "platform.h"
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <stdarg.h>
  17. #include <assert.h>
  18. #include <string.h>
  19. #include <ctype.h>
  20. #include <time.h>
  21. #include <math.h>
  22. #include "jstring.hpp"
  23. #include "jexcept.hpp"
  24. #include "jhash.hpp"
  25. #include "jlog.hpp"
  26. #include "jfile.hpp"
  27. #include "jdebug.hpp"
  28. #include "jutil.hpp"
  29. #define DOUBLE_FORMAT "%.16g"
  30. #define FLOAT_FORMAT "%.7g"
  31. #ifndef va_copy
  32. /* WARNING - DANGER - ASSUMES TYPICAL STACK MACHINE */
  33. #define va_copy(dst, src) ((void)((dst) = (src)))
  34. #endif
  35. static const char * TheNullStr = "";
  36. #define FIRST_CHUNK_SIZE 8
  37. #define DOUBLE_LIMIT 0x100000 // must be a power of 2
  38. #define DETACH_GRANULARITY 16
  39. //===========================================================================
  40. StringBuffer::StringBuffer()
  41. {
  42. init();
  43. }
  44. #if 0
  45. StringBuffer::StringBuffer(int initial)
  46. {
  47. init();
  48. ensureCapacity(initial);
  49. }
  50. #endif
  51. StringBuffer::StringBuffer(String & value)
  52. {
  53. init();
  54. append(value);
  55. }
  56. StringBuffer::StringBuffer(const char *value)
  57. {
  58. init();
  59. append(value);
  60. }
  61. StringBuffer::StringBuffer(char value)
  62. {
  63. init();
  64. append(value);
  65. }
  66. StringBuffer::StringBuffer(unsigned len, const char *value)
  67. {
  68. init();
  69. append(len, value);
  70. }
  71. StringBuffer::StringBuffer(const StringBuffer & value)
  72. {
  73. init();
  74. append(value);
  75. }
  76. StringBuffer::StringBuffer(bool useInternal)
  77. {
  78. if (useInternal)
  79. init();
  80. else
  81. initNoInternal();
  82. }
  83. StringBuffer::~StringBuffer()
  84. {
  85. freeBuffer();
  86. }
  87. void StringBuffer::freeBuffer()
  88. {
  89. if (buffer != internalBuffer)
  90. free(buffer);
  91. }
  92. void StringBuffer::setBuffer(size32_t buffLen, char * newBuff, size32_t strLen)
  93. {
  94. assertex(newBuff);
  95. assertex(buffLen>0 && strLen<buffLen);
  96. freeBuffer();
  97. buffer = newBuff;
  98. maxLen = buffLen;
  99. curLen = strLen;
  100. }
  101. void StringBuffer::_realloc(size32_t newLen)
  102. {
  103. if (newLen >= maxLen)
  104. {
  105. size32_t newMax = maxLen;
  106. if (newMax == 0)
  107. newMax = FIRST_CHUNK_SIZE;
  108. if (newLen > DOUBLE_LIMIT)
  109. {
  110. newMax = (newLen + DOUBLE_LIMIT) & ~(DOUBLE_LIMIT-1);
  111. if (newLen >= newMax)
  112. throw MakeStringException(MSGAUD_operator, -1, "StringBuffer::_realloc: Request for %d bytes oldMax = %d", newLen, maxLen);
  113. }
  114. else
  115. {
  116. while (newLen >= newMax)
  117. newMax += newMax;
  118. }
  119. char * newStr;
  120. char * originalBuffer = (buffer == internalBuffer) ? NULL : buffer;
  121. if (!newMax || !(newStr=(char *)realloc(originalBuffer, newMax)))
  122. {
  123. DBGLOG("StringBuffer::_realloc: Failed to realloc = %d, oldMax = %d", newMax, maxLen);
  124. PrintStackReport();
  125. PrintMemoryReport();
  126. throw MakeStringException(MSGAUD_operator, -1, "StringBuffer::_realloc: Failed to realloc = %d, oldMax = %d", newMax, maxLen);
  127. }
  128. if (useInternal())
  129. memcpy(newStr, internalBuffer, curLen);
  130. buffer = newStr;
  131. maxLen = newMax;
  132. }
  133. }
  134. char * StringBuffer::detach()
  135. {
  136. dbgassertex(buffer);
  137. char * result;
  138. if (buffer == internalBuffer)
  139. {
  140. result = (char *)malloc(curLen+1);
  141. memcpy(result, buffer, curLen);
  142. }
  143. else
  144. {
  145. if (maxLen>curLen+1+DETACH_GRANULARITY)
  146. buffer = (char *)realloc(buffer,curLen+1); // shrink
  147. result = buffer;
  148. }
  149. result[curLen] = '\0'; // There is always room for this null
  150. init();
  151. return result;
  152. }
  153. StringBuffer & StringBuffer::append(char value)
  154. {
  155. ensureCapacity(1);
  156. buffer[curLen] = value;
  157. ++curLen;
  158. return *this;
  159. }
  160. StringBuffer & StringBuffer::append(unsigned char value)
  161. {
  162. ensureCapacity(1);
  163. buffer[curLen] = value;
  164. ++curLen;
  165. return *this;
  166. }
  167. StringBuffer & StringBuffer::append(const char * value)
  168. {
  169. if (value)
  170. {
  171. size32_t SourceLen = (size32_t)::strlen(value);
  172. ensureCapacity(SourceLen);
  173. memcpy(buffer + curLen, value, SourceLen);
  174. curLen += SourceLen;
  175. }
  176. return *this;
  177. }
  178. StringBuffer & StringBuffer::append(unsigned len, const char * value)
  179. {
  180. if (len)
  181. {
  182. ensureCapacity(len);
  183. memcpy(buffer + curLen, value, len);
  184. curLen += len;
  185. }
  186. return *this;
  187. }
  188. StringBuffer & StringBuffer::append(const unsigned char * value)
  189. {
  190. return append((const char *) value);
  191. }
  192. StringBuffer & StringBuffer::append(const char * value, int offset, int len)
  193. {
  194. ensureCapacity(len);
  195. memcpy(buffer + curLen, value+offset, len);
  196. curLen += len;
  197. return *this;
  198. }
  199. StringBuffer & StringBuffer::append(const IAtom * value)
  200. {
  201. if (value)
  202. append(value->queryStr());
  203. return *this;
  204. }
  205. StringBuffer & StringBuffer::append(double value)
  206. {
  207. int len = length();
  208. int newlen = appendf(DOUBLE_FORMAT, value).length();
  209. while (len < newlen)
  210. {
  211. switch (charAt(len))
  212. {
  213. case '.':
  214. case 'E':
  215. case 'e':
  216. case 'N': // Not a number/infinity
  217. case 'n':
  218. return *this;
  219. }
  220. len++;
  221. }
  222. return append(".0");
  223. }
  224. StringBuffer & StringBuffer::append(float value)
  225. {
  226. int len = length();
  227. int newlen = appendf(FLOAT_FORMAT, value).length();
  228. while (len < newlen)
  229. {
  230. switch (charAt(len))
  231. {
  232. case '.':
  233. case 'E':
  234. case 'e':
  235. case 'N': // Not a number/infinity
  236. case 'n':
  237. return *this;
  238. }
  239. len++;
  240. }
  241. return append(".0");
  242. }
  243. StringBuffer & StringBuffer::append(int value)
  244. {
  245. char temp[12];
  246. unsigned written = numtostr(temp, value);
  247. return append(written, temp);
  248. }
  249. StringBuffer & StringBuffer::append(unsigned value)
  250. {
  251. char temp[12];
  252. unsigned written = numtostr(temp, value);
  253. return append(written, temp);
  254. }
  255. StringBuffer & StringBuffer::appendlong(long value)
  256. {
  257. char temp[24];
  258. unsigned written = numtostr(temp, value);
  259. return append(written, temp);
  260. }
  261. StringBuffer & StringBuffer::appendulong(unsigned long value)
  262. {
  263. char temp[24];
  264. unsigned written = numtostr(temp, value);
  265. return append(written, temp);
  266. }
  267. StringBuffer & StringBuffer::append(__int64 value)
  268. {
  269. char temp[24];
  270. unsigned written = numtostr(temp, value);
  271. return append(written, temp);
  272. }
  273. StringBuffer & StringBuffer::append(unsigned __int64 value)
  274. {
  275. char temp[24];
  276. unsigned written = numtostr(temp, value);
  277. return append(written, temp);
  278. }
  279. StringBuffer & StringBuffer::append(const String & value)
  280. {
  281. size32_t SourceLen = value.length();
  282. ensureCapacity(SourceLen);
  283. value.getChars(0, SourceLen, buffer, curLen);
  284. curLen += SourceLen;
  285. return *this;
  286. }
  287. StringBuffer & StringBuffer::append(const IStringVal & value)
  288. {
  289. return append(value.str());
  290. }
  291. StringBuffer & StringBuffer::append(const IStringVal * value)
  292. {
  293. if (value)
  294. return append(value->str());
  295. else
  296. return *this;
  297. }
  298. StringBuffer & StringBuffer::append(const StringBuffer & value)
  299. {
  300. size32_t SourceLen = value.length();
  301. ensureCapacity(SourceLen);
  302. value.getChars(0, SourceLen, buffer + curLen);
  303. curLen += SourceLen;
  304. return *this;
  305. }
  306. StringBuffer & StringBuffer::appendf(const char *format, ...)
  307. {
  308. va_list args;
  309. va_start(args, format);
  310. valist_appendf(format, args);
  311. va_end(args);
  312. return *this;
  313. }
  314. StringBuffer & StringBuffer::appendLower(unsigned len, const char * value)
  315. {
  316. if (len)
  317. {
  318. ensureCapacity(len);
  319. const byte * from = reinterpret_cast<const byte *>(value);
  320. for (unsigned i = 0; i < len; i++)
  321. buffer[curLen + i] = tolower(from[i]);
  322. curLen += len;
  323. }
  324. return *this;
  325. }
  326. StringBuffer & StringBuffer::setf(const char *format, ...)
  327. {
  328. clear();
  329. va_list args;
  330. va_start(args, format);
  331. valist_appendf(format, args);
  332. va_end(args);
  333. return *this;
  334. }
  335. StringBuffer & StringBuffer::limited_valist_appendf(unsigned szLimit, const char *format, va_list args)
  336. {
  337. #define BUF_SIZE 1024
  338. #define MAX_BUF_SIZE (1024*1024) // limit buffer size to 1MB when doubling
  339. // handle string that is bigger that BUF_SIZE bytes
  340. unsigned size = (0 == szLimit||szLimit>BUF_SIZE)?BUF_SIZE:szLimit;
  341. int len;
  342. va_list args2;
  343. va_copy(args2, args);
  344. try { ensureCapacity(size); }
  345. catch (IException *e)
  346. {
  347. StringBuffer eMsg;
  348. IException *e2 = MakeStringException(-1, "StringBuffer::valist_appendf(\"%s\"): vsnprintf failed or result exceeds limit (%d): %s", format, size, e->errorMessage(eMsg).str());
  349. e->Release();
  350. throw e2;
  351. }
  352. len = _vsnprintf(buffer+curLen,size,format,args);
  353. if (len >= 0)
  354. {
  355. if ((unsigned)len >= size)
  356. {
  357. if (szLimit && (unsigned)len >= szLimit)
  358. {
  359. if ((unsigned)len>szLimit)
  360. {
  361. len = size;
  362. if (len>3) memcpy(buffer+len-3, "...", 3);
  363. }
  364. }
  365. else
  366. {
  367. ensureCapacity(len);
  368. // no need for _vsnprintf since the buffer is already made big enough
  369. vsprintf(buffer+curLen,format,args2);
  370. }
  371. }
  372. }
  373. else if (size == szLimit)
  374. {
  375. len = size;
  376. if (len>3) memcpy(buffer+len-3, "...", 3);
  377. }
  378. else
  379. {
  380. size = BUF_SIZE * 2;
  381. loop
  382. {
  383. if (0 != szLimit && size>szLimit) size = szLimit; // if so, will be last attempt
  384. if (size>MAX_BUF_SIZE)
  385. {
  386. WARNLOG("StringBuffer::valist_appendf(\"%s\"): vsnprintf exceeds limit (%d)", format, size);
  387. size = szLimit = MAX_BUF_SIZE;
  388. }
  389. try { ensureCapacity(size); }
  390. catch (IException *e)
  391. {
  392. StringBuffer eMsg;
  393. IException *e2 = MakeStringException(-1, "StringBuffer::valist_appendf(\"%s\"): vsnprintf failed (%d): %s", format, size, e->errorMessage(eMsg).str());
  394. e->Release();
  395. throw e2;
  396. }
  397. va_list args3;
  398. va_copy(args3, args2);
  399. len = _vsnprintf(buffer+curLen,size,format,args3);
  400. va_end(args3);
  401. if (len>=0) // NB: len>size not possible, 1st _vsnprintf would have handled.
  402. break;
  403. if (size == szLimit)
  404. {
  405. len = size;
  406. if (len>3) memcpy(buffer+len-3, "...", 3);
  407. break;
  408. }
  409. size <<= 1;
  410. }
  411. }
  412. va_end(args2);
  413. curLen += len;
  414. return *this;
  415. }
  416. StringBuffer & StringBuffer::appendN(size32_t count, char fill)
  417. {
  418. ensureCapacity(count);
  419. memset(buffer+curLen, fill, count);
  420. curLen += count;
  421. return *this;
  422. }
  423. void StringBuffer::setLength(unsigned len)
  424. {
  425. if (len > curLen)
  426. {
  427. ensureCapacity(len-curLen);
  428. }
  429. curLen = len;
  430. }
  431. char * StringBuffer::reserve(size32_t size)
  432. {
  433. ensureCapacity(size);
  434. char *ret = buffer+curLen;
  435. curLen += size;
  436. return ret;
  437. }
  438. char * StringBuffer::reserveTruncate(size32_t size)
  439. {
  440. size32_t newMax = curLen+size+1;
  441. if (buffer == internalBuffer)
  442. {
  443. if (newMax > InternalBufferSize)
  444. {
  445. char * newStr = (char *)malloc(newMax);
  446. if (!newStr)
  447. throw MakeStringException(-1, "StringBuffer::_realloc: Failed to realloc newMax = %d, oldMax = %d", newMax, maxLen);
  448. memcpy(newStr, buffer, curLen);
  449. buffer = newStr;
  450. maxLen = newMax;
  451. }
  452. }
  453. else if (newMax != maxLen)
  454. {
  455. char * newStr = (char *) realloc(buffer, newMax);
  456. if (!newStr)
  457. throw MakeStringException(-1, "StringBuffer::_realloc: Failed to realloc newMax = %d, oldMax = %d", newMax, maxLen);
  458. buffer = newStr;
  459. maxLen = newMax;
  460. }
  461. char *ret = buffer+curLen;
  462. curLen += size;
  463. return ret;
  464. }
  465. void StringBuffer::swapWith(StringBuffer &other)
  466. {
  467. //Swap max
  468. size_t tempMax = maxLen;
  469. maxLen = other.maxLen;
  470. other.maxLen = tempMax;
  471. //Swap lengths
  472. size32_t thisLen = curLen;
  473. size32_t otherLen = other.curLen;
  474. curLen = otherLen;
  475. other.curLen = thisLen;
  476. //Swap buffers
  477. char * thisBuffer = buffer;
  478. char * otherBuffer = other.buffer;
  479. if (useInternal())
  480. {
  481. if (other.useInternal())
  482. {
  483. //NOTE: The c++ compiler can generate better code for the fixed size memcpy than it can
  484. // if only the required characters are copied.
  485. char temp[InternalBufferSize];
  486. memcpy(temp, thisBuffer, InternalBufferSize);
  487. memcpy(thisBuffer, otherBuffer, InternalBufferSize);
  488. memcpy(otherBuffer, temp, InternalBufferSize);
  489. //buffers already point in the correct place
  490. }
  491. else
  492. {
  493. memcpy(other.internalBuffer, thisBuffer, InternalBufferSize);
  494. buffer = otherBuffer;
  495. other.buffer = other.internalBuffer;
  496. }
  497. }
  498. else
  499. {
  500. if (other.useInternal())
  501. {
  502. memcpy(internalBuffer, otherBuffer, InternalBufferSize);
  503. buffer = internalBuffer;
  504. other.buffer = thisBuffer;
  505. }
  506. else
  507. {
  508. buffer = otherBuffer;
  509. other.buffer = thisBuffer;
  510. }
  511. }
  512. }
  513. void StringBuffer::setown(StringBuffer &other)
  514. {
  515. maxLen = other.maxLen;
  516. curLen = other.curLen;
  517. freeBuffer();
  518. if (other.useInternal())
  519. {
  520. memcpy(internalBuffer, other.internalBuffer, InternalBufferSize);
  521. buffer = internalBuffer;
  522. }
  523. else
  524. {
  525. buffer = other.buffer;
  526. }
  527. other.init();
  528. }
  529. void StringBuffer::kill()
  530. {
  531. freeBuffer();
  532. init();
  533. }
  534. void StringBuffer::getChars(int srcBegin, int srcEnd, char * target) const
  535. {
  536. if (srcBegin < 0)
  537. srcBegin = 0;
  538. if ((unsigned)srcEnd > curLen)
  539. srcEnd = curLen;
  540. const int len = srcEnd - srcBegin;
  541. if (target && buffer && len > 0)
  542. memcpy(target, buffer + srcBegin, len);
  543. }
  544. void StringBuffer::_insert(unsigned offset, size32_t insertLen)
  545. {
  546. ensureCapacity(insertLen);
  547. memmove(buffer + offset + insertLen, buffer + offset, curLen - offset);
  548. curLen += insertLen;
  549. }
  550. StringBuffer & StringBuffer::insert(int offset, char value)
  551. {
  552. _insert(offset, 1);
  553. buffer[offset] = value;
  554. return *this;
  555. }
  556. StringBuffer & StringBuffer::insert(int offset, const char * value)
  557. {
  558. if (!value) return *this;
  559. unsigned len = (size32_t)strlen(value);
  560. _insert(offset, len);
  561. memcpy(buffer + offset, value, len);
  562. return *this;
  563. }
  564. StringBuffer & StringBuffer::insert(int offset, double value)
  565. {
  566. char temp[36];
  567. sprintf(temp, "%f", value);
  568. insert(offset, temp);
  569. return *this;
  570. }
  571. StringBuffer & StringBuffer::insert(int offset, float value)
  572. {
  573. return insert(offset, (double)value);
  574. }
  575. StringBuffer & StringBuffer::insert(int offset, int value)
  576. {
  577. char temp[12];
  578. numtostr(temp, value);
  579. return insert(offset, temp);
  580. }
  581. StringBuffer & StringBuffer::insert(int offset, unsigned value)
  582. {
  583. char temp[12];
  584. numtostr(temp, value);
  585. return insert(offset, temp);
  586. }
  587. #if 0
  588. StringBuffer & StringBuffer::insert(int offset, long value)
  589. {
  590. char temp[24];
  591. numtostr(temp, value);
  592. return insert(offset, temp);
  593. }
  594. #endif
  595. StringBuffer & StringBuffer::insert(int offset, __int64 value)
  596. {
  597. char temp[24];
  598. numtostr(temp, value);
  599. return insert(offset, temp);
  600. }
  601. StringBuffer & StringBuffer::insert(int offset, const String & value)
  602. {
  603. size32_t len = value.length();
  604. _insert(offset, len);
  605. value.getChars(0, len, buffer, offset);
  606. return *this;
  607. }
  608. StringBuffer & StringBuffer::insert(int offset, const StringBuffer & value)
  609. {
  610. size32_t len = value.length();
  611. _insert(offset, len);
  612. value.getChars(0, len, buffer+offset);
  613. return *this;
  614. }
  615. StringBuffer & StringBuffer::insert(int offset, const IStringVal & value)
  616. {
  617. return insert(offset, value.str());
  618. }
  619. StringBuffer & StringBuffer::insert(int offset, const IStringVal * value)
  620. {
  621. if (value)
  622. return insert(offset, value->str());
  623. else
  624. return *this;
  625. }
  626. StringBuffer & StringBuffer::newline()
  627. {
  628. return append("\n");
  629. }
  630. StringBuffer & StringBuffer::pad(unsigned count)
  631. {
  632. ensureCapacity(count);
  633. memset(buffer + curLen, ' ', count);
  634. curLen += count;
  635. return *this;
  636. }
  637. StringBuffer & StringBuffer::padTo(unsigned count)
  638. {
  639. if (curLen<count)
  640. pad(count-curLen);
  641. return *this;
  642. }
  643. StringBuffer & StringBuffer::clip()
  644. {
  645. while (curLen && isspace(buffer[curLen-1]))
  646. curLen--;
  647. return *this;
  648. }
  649. StringBuffer & StringBuffer::trim()
  650. {
  651. return clip().trimLeft();
  652. }
  653. StringBuffer & StringBuffer::trimLeft()
  654. {
  655. char *p;
  656. if (curLen==0)
  657. return *this;
  658. buffer[curLen] = 0;
  659. for(p = buffer;isspace(*p);p++)
  660. ;
  661. if (p!=buffer)
  662. {
  663. curLen -= p-buffer;
  664. memmove(buffer,p,curLen);
  665. }
  666. return *this;
  667. }
  668. StringBuffer & StringBuffer::remove(unsigned start, unsigned len)
  669. {
  670. if (start > curLen) start = curLen;
  671. if (start + len > curLen) len = curLen - start;
  672. unsigned start2 = start + len;
  673. memmove(buffer + start, buffer + start2, curLen - start2);
  674. setLength(curLen - len);
  675. return *this;
  676. }
  677. StringBuffer &StringBuffer::reverse()
  678. {
  679. unsigned max = curLen/2;
  680. char * end = buffer + curLen;
  681. unsigned idx;
  682. for (idx = 0; idx < max; idx++)
  683. {
  684. char temp = buffer[idx];
  685. end--;
  686. buffer[idx] = *end;
  687. *end = temp;
  688. }
  689. return *this;
  690. }
  691. MemoryBuffer & StringBuffer::deserialize(MemoryBuffer & in)
  692. {
  693. unsigned len;
  694. in.read(len);
  695. append(len, (const char *)in.readDirect(len));
  696. return in;
  697. }
  698. MemoryBuffer & StringBuffer::serialize(MemoryBuffer & out) const
  699. {
  700. return out.append(curLen).append(curLen, buffer);
  701. }
  702. StringBuffer &StringBuffer::loadFile(const char *filename, bool binaryMode)
  703. {
  704. FILE *in = fopen(filename, binaryMode?"rb":"rt");
  705. if (in)
  706. {
  707. char buffer[1024];
  708. int bytes;
  709. for (;;)
  710. {
  711. bytes = (size32_t)fread(buffer, 1, sizeof(buffer), in);
  712. if (!bytes)
  713. break;
  714. append(buffer, 0, bytes);
  715. }
  716. fclose(in);
  717. return *this;
  718. }
  719. else
  720. throw MakeStringException(errno, "File %s could not be opened", filename);
  721. }
  722. StringBuffer & StringBuffer::loadFile(IFile* f)
  723. {
  724. if(!f)
  725. return *this;
  726. Owned<IFileIO> io = f->open(IFOread);
  727. if(!io)
  728. throw MakeStringException(errno, "file %s could not be opened for reading", f->queryFilename());
  729. char buf[2048];
  730. const unsigned requestedSize = sizeof(buf);
  731. offset_t pos = 0;
  732. loop
  733. {
  734. size32_t len = io->read(pos, requestedSize, buf);
  735. if (len == 0)
  736. break;
  737. append(len, buf);
  738. pos += len;
  739. if (len != requestedSize)
  740. break;
  741. }
  742. return *this;
  743. }
  744. void StringBuffer::setCharAt(unsigned offset, char value)
  745. {
  746. if (offset < curLen)
  747. buffer[offset] = value;
  748. }
  749. StringBuffer & StringBuffer::toLowerCase()
  750. {
  751. size32_t l = curLen;
  752. for (size32_t i = 0; i < l; i++)
  753. {
  754. if (isupper(buffer[i]))
  755. buffer[i] = tolower(buffer[i]);
  756. }
  757. return *this;
  758. }
  759. StringBuffer & StringBuffer::toUpperCase()
  760. {
  761. size32_t l = curLen;
  762. for (size32_t i = 0; i < l; i++)
  763. {
  764. if (islower(buffer[i]))
  765. buffer[i] = toupper(buffer[i]);
  766. }
  767. return *this;
  768. }
  769. StringBuffer & StringBuffer::replace(char oldChar, char newChar)
  770. {
  771. size32_t l = curLen;
  772. for (size32_t i = 0; i < l; i++)
  773. {
  774. if (buffer[i] == oldChar)
  775. {
  776. buffer[i] = newChar;
  777. if (newChar == '\0')
  778. {
  779. curLen = i;
  780. break;
  781. }
  782. }
  783. }
  784. return *this;
  785. }
  786. // this method will replace all occurrances of "oldStr" with "newStr"
  787. StringBuffer & StringBuffer::replaceString(const char* oldStr, const char* newStr)
  788. {
  789. if (curLen)
  790. {
  791. const char* s = str(); // get null terminated version of the string
  792. int left = length();
  793. int oldStr_len = (size32_t)strlen(oldStr);
  794. StringBuffer tempbuff;
  795. while (left >= oldStr_len)
  796. {
  797. if ( memcmp(s, oldStr, oldStr_len) == 0)
  798. {
  799. tempbuff.append(newStr);
  800. s += oldStr_len;
  801. left -= oldStr_len;
  802. }
  803. else
  804. {
  805. tempbuff.append(*s);
  806. s++;
  807. left--;
  808. }
  809. }
  810. // there are no more possible replacements, make sure we keep the end of the original buffer
  811. tempbuff.append(s);
  812. //*this = tempbuff;
  813. swapWith(tempbuff);
  814. }
  815. return *this;
  816. }
  817. StringBuffer & StringBuffer::stripChar(char oldChar)
  818. {
  819. size32_t delta = 0;
  820. size32_t l = curLen;
  821. for (size32_t i = 0; i < l; i++)
  822. {
  823. if (buffer[i] == oldChar)
  824. delta++;
  825. else if (delta)
  826. buffer[i-delta] = buffer[i];
  827. }
  828. if (delta)
  829. curLen = curLen - delta;
  830. return *this;
  831. }
  832. const char * StringBuffer::str() const
  833. {
  834. buffer[curLen] = '\0'; // There is always room for this null
  835. return buffer;
  836. }
  837. //===========================================================================
  838. VStringBuffer::VStringBuffer(const char* format, ...)
  839. {
  840. va_list args;
  841. va_start(args,format);
  842. valist_appendf(format,args);
  843. va_end(args);
  844. }
  845. //===========================================================================
  846. StringAttrBuilder::StringAttrBuilder(StringAttr & _target) : StringBuffer(false), target(_target)
  847. {
  848. }
  849. StringAttrBuilder::~StringAttrBuilder()
  850. {
  851. target.setown(*this);
  852. }
  853. //===========================================================================
  854. String::String()
  855. {
  856. text = (char *)TheNullStr;
  857. }
  858. String::String(const char * value)
  859. {
  860. text = (value ? strdup(value) : (char *)TheNullStr);
  861. }
  862. String::String(const char * value, int offset, int _count)
  863. {
  864. text = (char *)malloc(_count+1);
  865. memcpy(text, value+offset, _count);
  866. text[_count]=0;
  867. }
  868. String::String(String & value)
  869. {
  870. text = strdup(value.str());
  871. }
  872. String::String(StringBuffer & value)
  873. {
  874. unsigned len = value.length();
  875. text = (char *)malloc(len+1);
  876. value.getChars(0,len,text);
  877. text[len] = 0;
  878. }
  879. String::~String()
  880. {
  881. if (text != TheNullStr) free(text);
  882. }
  883. char String::charAt(size32_t index) const
  884. {
  885. return text[index];
  886. }
  887. int String::compareTo(const String & value) const
  888. {
  889. return strcmp(text, value.str());
  890. }
  891. int String::compareTo(const char* value) const
  892. {
  893. return strcmp(text,value);
  894. }
  895. String * String::concat(const String & value) const
  896. {
  897. StringBuffer temp(str());
  898. temp.append(value);
  899. return new String(temp.str());
  900. }
  901. bool String::endsWith(const String & value) const
  902. {
  903. unsigned lenValue = value.length();
  904. unsigned len = (size32_t)strlen(text);
  905. if (len >= lenValue)
  906. return (memcmp(text+(len-lenValue),value.str(),lenValue) == 0);
  907. return false;
  908. }
  909. bool String::endsWith(const char* value) const
  910. {
  911. return ::endsWith(this->text, value);
  912. }
  913. bool String::equals(String & value) const
  914. {
  915. return strcmp(text, value.str())==0;
  916. }
  917. bool String::equalsIgnoreCase(const String & value) const
  918. {
  919. return stricmp(text, value.str())==0;
  920. }
  921. void String::getBytes(int srcBegin, int srcEnd, void * dest, int dstBegin) const
  922. {
  923. memcpy((char *)dest+dstBegin, text+srcBegin, srcEnd-srcBegin);
  924. }
  925. void String::getChars(int srcBegin, int srcEnd, void * dest, int dstBegin) const
  926. {
  927. memcpy((char *)dest+dstBegin, text+srcBegin, srcEnd-srcBegin);
  928. }
  929. int String::hashCode() const
  930. {
  931. return (int)hashc((const byte *)text,length(),0);
  932. }
  933. int String::indexOf(int ch) const
  934. {
  935. char * match = strchr(text, ch);
  936. return match ? (int)(match - text) : -1;
  937. }
  938. int String::indexOf(int ch, int from) const
  939. {
  940. char * match = strchr(text + from, ch);
  941. return match ? (int)(match - text) : -1;
  942. }
  943. int String::indexOf(const String & search) const
  944. {
  945. const char * str = search.str();
  946. const char * match = strstr(text, str);
  947. return match ? (int)(match - text) : -1;
  948. }
  949. int String::indexOf(const String & search, int from) const
  950. {
  951. const char * str = search.str();
  952. const char * match = strstr(text + from, str);
  953. return match ? (int)(match - text) : -1;
  954. }
  955. int String::lastIndexOf(int ch) const
  956. {
  957. char * match = strrchr(text, ch);
  958. return match ? (int)(match - text) : -1;
  959. }
  960. int String::lastIndexOf(int ch, int from) const
  961. {
  962. for (;(from > 0);--from)
  963. if (text[from] == ch)
  964. return from;
  965. return -1;
  966. }
  967. int String::lastIndexOf(const String & search) const
  968. {
  969. assertex(!"TBD");
  970. return -1;
  971. }
  972. int String::lastIndexOf(const String & search, int from) const
  973. {
  974. assertex(!"TBD");
  975. return -1;
  976. }
  977. size32_t String::length() const
  978. {
  979. return (size32_t)strlen(text);
  980. }
  981. bool String::startsWith(String & value) const
  982. {
  983. unsigned lenValue = value.length();
  984. const char * search = value.str();
  985. return (memcmp(text, search, lenValue) == 0);
  986. }
  987. bool String::startsWith(String & value, int offset) const
  988. {
  989. unsigned lenValue = value.length();
  990. const char * search = value.str();
  991. return (memcmp(text + offset, search, lenValue) == 0);
  992. }
  993. bool String::startsWith(const char* value) const
  994. {
  995. return ::startsWith(this->text,value);
  996. }
  997. String * String::substring(int beginIndex) const
  998. {
  999. return new String(text+beginIndex);
  1000. }
  1001. String * String::substring(int beginIndex, int endIndex) const
  1002. {
  1003. return new String(text, beginIndex, endIndex - beginIndex);
  1004. }
  1005. const char *String::str() const
  1006. {
  1007. return text;
  1008. }
  1009. String * String::toLowerCase() const
  1010. {
  1011. String *ret = new String();
  1012. size32_t l = length();
  1013. if (l)
  1014. {
  1015. ret->text = (char *)malloc(l+1);
  1016. for (unsigned i = 0; i < l; i++)
  1017. ret->text[i] = tolower(text[i]);
  1018. ret->text[l]=0;
  1019. }
  1020. return ret;
  1021. }
  1022. String * String::toString()
  1023. {
  1024. Link();
  1025. return this;
  1026. }
  1027. String * String::toUpperCase() const
  1028. {
  1029. String *ret = new String();
  1030. size32_t l = length();
  1031. if (l)
  1032. {
  1033. ret->text = (char *)malloc(l+1);
  1034. for (unsigned i = 0; i < l; i++)
  1035. ret->text[i] = toupper(text[i]);
  1036. ret->text[l]=0;
  1037. }
  1038. return ret;
  1039. }
  1040. String * String::trim() const
  1041. {
  1042. size32_t l = length();
  1043. while (l && isspace(text[l-1]))
  1044. l--;
  1045. return new String(text, 0, l);
  1046. }
  1047. //------------------------------------------------
  1048. #if 0
  1049. String & String::valueOf(char value)
  1050. {
  1051. return * new String(&value, 0, 1);
  1052. }
  1053. String & String::valueOf(const char * value)
  1054. {
  1055. return * new String(value);
  1056. }
  1057. String & String::valueOf(const char * value, int offset, int count)
  1058. {
  1059. return * new String(value, offset, count);
  1060. }
  1061. String & String::valueOf(double value)
  1062. {
  1063. StringBuffer temp;
  1064. return temp.append(value).toString();
  1065. }
  1066. String & String::valueOf(float value)
  1067. {
  1068. StringBuffer temp;
  1069. return temp.append(value).toString();
  1070. }
  1071. String & String::valueOf(int value)
  1072. {
  1073. StringBuffer temp;
  1074. return temp.append(value).toString();
  1075. }
  1076. String & String::valueOf(long value)
  1077. {
  1078. StringBuffer temp;
  1079. return temp.append(value).toString();
  1080. }
  1081. #endif
  1082. //------------------------------------------------
  1083. StringAttr::StringAttr(const char * _text)
  1084. {
  1085. text = _text ? strdup(_text) : NULL;
  1086. }
  1087. StringAttr::StringAttr(const char * _text, unsigned _len)
  1088. {
  1089. text = NULL;
  1090. set(_text, _len);
  1091. }
  1092. StringAttr::StringAttr(const StringAttr & src)
  1093. {
  1094. text = NULL;
  1095. set(src.get());
  1096. }
  1097. void StringAttr::set(const char * _text)
  1098. {
  1099. free(text);
  1100. text = _text ? strdup(_text) : NULL;
  1101. }
  1102. void StringAttr::set(const char * _text, unsigned _len)
  1103. {
  1104. if (text)
  1105. free(text);
  1106. text = (char *)malloc(_len+1);
  1107. memcpy(text, _text, _len);
  1108. text[_len] = 0;
  1109. }
  1110. void StringAttr::setown(const char * _text)
  1111. {
  1112. if (text)
  1113. free(text);
  1114. text = (char *)_text;
  1115. }
  1116. void StringAttr::set(const StringBuffer & source)
  1117. {
  1118. if (source.length())
  1119. set(source.str());
  1120. else
  1121. clear();
  1122. }
  1123. void StringAttr::setown(StringBuffer & source)
  1124. {
  1125. if (source.length())
  1126. setown(source.detach());
  1127. else
  1128. clear();
  1129. }
  1130. void StringAttr::toLowerCase()
  1131. {
  1132. if (text)
  1133. {
  1134. char * cur = text;
  1135. char next;
  1136. while ((next = *cur) != 0)
  1137. {
  1138. if (isupper(next))
  1139. *cur = tolower(next);
  1140. cur++;
  1141. }
  1142. }
  1143. }
  1144. void StringAttr::toUpperCase()
  1145. {
  1146. if (text)
  1147. {
  1148. char * cur = text;
  1149. char next;
  1150. while ((next = *cur) != 0)
  1151. {
  1152. if (islower(next))
  1153. *cur = toupper(next);
  1154. cur++;
  1155. }
  1156. }
  1157. }
  1158. StringAttrItem::StringAttrItem(const char *_text, unsigned _len)
  1159. {
  1160. text.set(_text, _len);
  1161. }
  1162. inline char hex(char c, char lower)
  1163. {
  1164. if (c < 10)
  1165. return '0' + c;
  1166. else if (lower)
  1167. return 'a' + c - 10;
  1168. else
  1169. return 'A' + c - 10;
  1170. }
  1171. StringBuffer & StringBuffer::appendhex(unsigned char c, char lower)
  1172. {
  1173. append(hex(c>>4, lower));
  1174. append(hex(c&0xF, lower));
  1175. return *this;
  1176. }
  1177. void appendURL(StringBuffer *dest, const char *src, size32_t len, char lower)
  1178. {
  1179. if (len == (size32_t)-1)
  1180. len = (size32_t)strlen(src);
  1181. while (len)
  1182. {
  1183. // isalnum seems to give weird results for chars > 127....
  1184. unsigned char c = (unsigned char) *src;
  1185. if (c == ' ')
  1186. dest->append('+');
  1187. else if ((c & 0x80) || !isalnum(*src))
  1188. {
  1189. dest->append('%');
  1190. dest->appendhex(c, lower);
  1191. }
  1192. else
  1193. dest->append(c);
  1194. src++;
  1195. len--;
  1196. }
  1197. }
  1198. inline char translateHex(char hex)
  1199. {
  1200. if(hex >= 'A')
  1201. return (hex & 0xdf) - 'A' + 10;
  1202. else
  1203. return hex - '0';
  1204. }
  1205. inline char translateHex(char h1, char h2)
  1206. {
  1207. return (translateHex(h1) * 16 + translateHex(h2));
  1208. }
  1209. StringBuffer &appendDecodedURL(StringBuffer &s, const char *url)
  1210. {
  1211. if(!url)
  1212. return s;
  1213. while (*url)
  1214. {
  1215. char c = *url++;
  1216. if (c == '+')
  1217. c = ' ';
  1218. else if (c == '%')
  1219. {
  1220. if (isxdigit(url[0]) && isxdigit(url[1]))
  1221. {
  1222. c = translateHex(url[0], url[1]);
  1223. url+=2;
  1224. }
  1225. }
  1226. s.append(c);
  1227. }
  1228. return s;
  1229. }
  1230. static StringBuffer & appendStringExpandControl(StringBuffer &out, unsigned len, const char * src, bool addBreak, bool isCpp, bool isUtf8)
  1231. {
  1232. const int minBreakPos = 0;
  1233. const int commaBreakPos = 70;
  1234. const int maxBreakPos = 120;
  1235. const char * startLine = src;
  1236. out.ensureCapacity(len+2);
  1237. for (; len > 0; --len)
  1238. {
  1239. unsigned char c = *src++;
  1240. bool insertBreak = false;
  1241. bool allowBreak = true;
  1242. switch (c)
  1243. {
  1244. case '\n':
  1245. {
  1246. out.append("\\n");
  1247. if (src-startLine > minBreakPos)
  1248. insertBreak = true;
  1249. break;
  1250. }
  1251. case ',':
  1252. {
  1253. out.append(c);
  1254. if (src-startLine > commaBreakPos)
  1255. insertBreak = true;
  1256. break;
  1257. }
  1258. case '\r': out.append("\\r"); break;
  1259. case '\t': out.append("\\t"); break;
  1260. case '"':
  1261. if (isCpp)
  1262. out.append("\\");
  1263. out.append(c);
  1264. break;
  1265. case '\'':
  1266. if (!isCpp)
  1267. out.append("\\");
  1268. out.append(c);
  1269. break;
  1270. case '\\': out.append("\\\\"); break;
  1271. case '?':
  1272. if (isCpp)
  1273. {
  1274. //stop trigraphs being generated.... quote the second ?
  1275. out.append(c);
  1276. if ((len!=1) && (*src == '?'))
  1277. {
  1278. out.append('\\');
  1279. allowBreak = false;
  1280. }
  1281. }
  1282. else
  1283. out.append(c);
  1284. break;
  1285. default:
  1286. if (isUtf8 || ((c >= ' ') && (c <= 126)))
  1287. out.append(c);
  1288. else
  1289. out.appendf("\\%03o", c);
  1290. break;
  1291. }
  1292. if (addBreak && (insertBreak || (allowBreak && src-startLine >= maxBreakPos)))
  1293. {
  1294. out.append("\"").newline().append("\t\t\"");
  1295. startLine = src;
  1296. }
  1297. }
  1298. return out;
  1299. }
  1300. StringBuffer & appendStringAsCPP(StringBuffer &out, unsigned len, const char * src, bool addBreak)
  1301. {
  1302. return appendStringExpandControl(out, len, src, addBreak, true, false);
  1303. }
  1304. StringBuffer & appendStringAsECL(StringBuffer &out, unsigned len, const char * src)
  1305. {
  1306. return appendStringExpandControl(out, len, src, false, false, false);
  1307. }
  1308. StringBuffer & appendUtf8AsECL(StringBuffer &out, unsigned len, const char * src)
  1309. {
  1310. return appendStringExpandControl(out, len, src, false, false, true);
  1311. }
  1312. StringBuffer & appendStringAsQuotedCPP(StringBuffer &out, unsigned len, const char * src, bool addBreak)
  1313. {
  1314. out.ensureCapacity(len+2);
  1315. out.append('\"');
  1316. appendStringAsCPP(out, len, src, addBreak);
  1317. return out.append('\"');
  1318. }
  1319. StringBuffer & appendStringAsQuotedECL(StringBuffer &out, unsigned len, const char * src)
  1320. {
  1321. out.ensureCapacity(len+2);
  1322. out.append('\'');
  1323. appendStringAsECL(out, len, src);
  1324. return out.append('\'');
  1325. }
  1326. void extractItem(StringBuffer & res, const char * src, const char * sep, int whichItem, bool caps)
  1327. {
  1328. bool isSeparator[256];
  1329. memset(isSeparator,0,sizeof(isSeparator));
  1330. unsigned char * finger = (unsigned char *)sep;
  1331. while (*finger !=0)
  1332. isSeparator[*finger++] = true;
  1333. isSeparator[0]=true;
  1334. finger = (unsigned char *)src;
  1335. unsigned char next;
  1336. loop
  1337. {
  1338. while (isSeparator[(next = *finger)])
  1339. {
  1340. if (next == 0) return;
  1341. finger++;
  1342. }
  1343. if (whichItem == 0)
  1344. {
  1345. while (!isSeparator[(next = *finger)])
  1346. {
  1347. if (caps)
  1348. next = toupper(next);
  1349. res.append(next);
  1350. finger++;
  1351. }
  1352. return;
  1353. }
  1354. while (!isSeparator[*finger])
  1355. finger++;
  1356. whichItem--;
  1357. }
  1358. }
  1359. int utf8CharLen(unsigned char ch)
  1360. {
  1361. //return 1 if this is an ascii character,
  1362. //or 0 if its not a valid utf-8 character
  1363. if (ch < 128)
  1364. return 1;
  1365. if (ch < 192)
  1366. return 0;
  1367. unsigned char len = 1;
  1368. for (unsigned char lead = ch << 1; (lead & 0x80); lead <<=1)
  1369. len++;
  1370. return len;
  1371. }
  1372. int utf8CharLen(const unsigned char *ch, unsigned maxsize)
  1373. {
  1374. //return 1 if this is an ascii character,
  1375. //or 0 if its not a valid utf-8 character
  1376. if (*ch < 128)
  1377. return 1;
  1378. unsigned char len = utf8CharLen(*ch);
  1379. if (len>maxsize)
  1380. return 0;
  1381. for (unsigned pos = 1; pos < len; pos++)
  1382. if ((ch[pos] < 128) || (ch[pos] >= 192))
  1383. return 0; //its not a valid utf-8 character after all
  1384. return len;
  1385. }
  1386. const char *encodeXML(const char *x, StringBuffer &ret, unsigned flags, unsigned len, bool utf8)
  1387. {
  1388. while (len)
  1389. {
  1390. switch(*x)
  1391. {
  1392. case '&':
  1393. ret.append("&amp;");
  1394. break;
  1395. case '<':
  1396. ret.append("&lt;");
  1397. break;
  1398. case '>':
  1399. ret.append("&gt;");
  1400. break;
  1401. case '\"':
  1402. ret.append("&quot;");
  1403. break;
  1404. case '\'':
  1405. ret.append("&apos;");
  1406. break;
  1407. case ' ':
  1408. ret.append(flags & ENCODE_SPACES?"&#32;":" ");
  1409. break;
  1410. case '\n':
  1411. ret.append(flags & ENCODE_NEWLINES?"&#10;":"\n");
  1412. break;
  1413. case '\r':
  1414. ret.append(flags & ENCODE_NEWLINES?"&#13;":"\r");
  1415. break;
  1416. case '\t':
  1417. ret.append(flags & ENCODE_SPACES?"&#9;":"\t");
  1418. break;
  1419. case '\0':
  1420. if (len == (unsigned) -1)
  1421. return x;
  1422. ret.append("&#xe000;"); // hack!!! Characters below 0x20 are not legal in strict xml, even encoded.
  1423. break;
  1424. default:
  1425. if (*x >= ' ' && ((byte)*x) < 128)
  1426. ret.append(*x);
  1427. else if (*x < ' ' && *x > 0)
  1428. ret.append("&#xe0").appendhex(*x, true).append(';'); // HACK
  1429. else if (utf8)
  1430. {
  1431. unsigned chlen = utf8CharLen((const unsigned char *)x);
  1432. if (chlen==0)
  1433. ret.append("&#").append((unsigned int)*(unsigned char *) x).append(';');
  1434. else
  1435. {
  1436. ret.append(*x);
  1437. while(--chlen)
  1438. {
  1439. if (len != (unsigned) -1)
  1440. len--;
  1441. ret.append(*(++x));
  1442. }
  1443. }
  1444. }
  1445. else
  1446. ret.append("&#").append((unsigned int)*(unsigned char *) x).append(';');
  1447. break;
  1448. }
  1449. if (len != (unsigned) -1)
  1450. len--;
  1451. ++x;
  1452. }
  1453. return x;
  1454. }
  1455. const char *encodeXML(const char *x, IIOStream &out, unsigned flags, unsigned len, bool utf8)
  1456. {
  1457. while (len)
  1458. {
  1459. switch(*x)
  1460. {
  1461. case '&':
  1462. writeStringToStream(out, "&amp;");
  1463. break;
  1464. case '<':
  1465. writeStringToStream(out, "&lt;");
  1466. break;
  1467. case '>':
  1468. writeStringToStream(out, "&gt;");
  1469. break;
  1470. case '\"':
  1471. writeStringToStream(out, "&quot;");
  1472. break;
  1473. case '\'':
  1474. writeStringToStream(out, "&apos;");
  1475. break;
  1476. case ' ':
  1477. writeStringToStream(out, flags & ENCODE_SPACES?"&#32;":" ");
  1478. break;
  1479. case '\n':
  1480. writeStringToStream(out, flags & ENCODE_NEWLINES?"&#10;":"\n");
  1481. break;
  1482. case '\r':
  1483. writeStringToStream(out, flags & ENCODE_NEWLINES?"&#13;":"\r");
  1484. break;
  1485. case '\t':
  1486. writeStringToStream(out, flags & ENCODE_SPACES?"&#9;":"\t");
  1487. break;
  1488. case '\0':
  1489. if (len == (unsigned) -1)
  1490. return x;
  1491. writeStringToStream(out, "&#xe000;"); // hack!!! Characters below 0x20 are not legal in strict xml, even encoded.
  1492. break;
  1493. default:
  1494. if (*x >= ' ' && ((byte)*x) < 128)
  1495. writeCharToStream(out, *x);
  1496. else if (*x < ' ' && *x > 0)
  1497. {
  1498. writeStringToStream(out, "&#xe0");
  1499. unsigned char c = *(unsigned char *)x;
  1500. writeCharToStream(out, hex(c>>4, true));
  1501. writeCharToStream(out, hex(c&0xF, true));
  1502. writeCharToStream(out, ';'); // HACK
  1503. }
  1504. else if (utf8)
  1505. {
  1506. int chlen = utf8CharLen((const unsigned char *)x);
  1507. if (chlen==0)
  1508. {
  1509. writeStringToStream(out, "&#");
  1510. char tmp[12];
  1511. unsigned written = numtostr(tmp, *(unsigned char *)x);
  1512. out.write(written, tmp);
  1513. writeCharToStream(out, ';');
  1514. }
  1515. else
  1516. {
  1517. writeCharToStream(out, *x);
  1518. while(--chlen)
  1519. {
  1520. if (len != (unsigned) -1)
  1521. len--;
  1522. writeCharToStream(out, *(++x));
  1523. }
  1524. }
  1525. }
  1526. else
  1527. {
  1528. writeStringToStream(out, "&#");
  1529. char tmp[12];
  1530. unsigned written = numtostr(tmp, *(unsigned char *)x);
  1531. out.write(written, tmp);
  1532. writeCharToStream(out, ';');
  1533. }
  1534. break;
  1535. }
  1536. if (len != (unsigned) -1)
  1537. len--;
  1538. ++x;
  1539. }
  1540. return x;
  1541. }
  1542. static void writeUtf8(unsigned c, StringBuffer &out)
  1543. {
  1544. if (c < 0x80)
  1545. out.append((char)c);
  1546. else if (c < 0x800)
  1547. {
  1548. out.append((char)(0xC0 | (c>>6)));
  1549. out.append((char)(0x80 | (c & 0x3F)));
  1550. }
  1551. else if (c < 0x10000)
  1552. {
  1553. out.append((char) (0xE0 | (c>>12)));
  1554. out.append((char) (0x80 | (c>>6 & 0x3F)));
  1555. out.append((char) (0x80 | (c & 0x3F)));
  1556. }
  1557. else if (c < 0x200000)
  1558. {
  1559. out.append((char) (0xF0 | (c>>18)));
  1560. out.append((char) (0x80 | (c>>12 & 0x3F)));
  1561. out.append((char) (0x80 | (c>>6 & 0x3F)));
  1562. out.append((char) (0x80 | (c & 0x3F)));
  1563. }
  1564. else if (c < 0x4000000)
  1565. {
  1566. out.append((char) (0xF8 | (c>>24)));
  1567. out.append((char) (0x80 | (c>>18 & 0x3F)));
  1568. out.append((char) (0x80 | (c>>12 & 0x3F)));
  1569. out.append((char) (0x80 | (c>>6 & 0x3F)));
  1570. out.append((char) (0x80 | (c & 0x3F)));
  1571. }
  1572. else if (c < 0x80000000)
  1573. {
  1574. out.append((char) (0xFC | (c>>30)));
  1575. out.append((char) (0x80 | (c>>24 & 0x3F)));
  1576. out.append((char) (0x80 | (c>>18 & 0x3F)));
  1577. out.append((char) (0x80 | (c>>12 & 0x3F)));
  1578. out.append((char) (0x80 | (c>>6 & 0x3F)));
  1579. out.append((char) (0x80 | (c & 0x3F)));
  1580. }
  1581. else
  1582. assertex(false);
  1583. }
  1584. #define JSONSTRICT
  1585. const char *decodeJSON(const char *j, StringBuffer &ret, unsigned len, const char **errMark)
  1586. {
  1587. if (!j)
  1588. return j;
  1589. if ((unsigned)-1 == len)
  1590. len = (unsigned)strlen(j);
  1591. try
  1592. {
  1593. for (const char *end = j+len; j<end && *j; j++)
  1594. {
  1595. if (*j!='\\')
  1596. ret.append(*j);
  1597. else
  1598. {
  1599. switch (*++j)
  1600. {
  1601. case 'u':
  1602. {
  1603. j++;
  1604. if (end-j>=4)
  1605. {
  1606. char *endptr;
  1607. StringAttr s(j, 4);
  1608. unsigned val = strtoul(s.get(), &endptr, 16);
  1609. if (endptr && !*endptr)
  1610. {
  1611. writeUtf8(val, ret);
  1612. j+=3;
  1613. break;
  1614. }
  1615. }
  1616. #ifdef JSONSTRICT
  1617. throw MakeStringException(-1, "invalid json \\u escaped sequence");
  1618. #endif
  1619. ret.append(*j);
  1620. break;
  1621. }
  1622. case '\"':
  1623. case '\\':
  1624. case '/':
  1625. ret.append(*j);
  1626. break;
  1627. case 'b':
  1628. ret.append('\b');
  1629. break;
  1630. case 'f':
  1631. ret.append('\f');
  1632. break;
  1633. case 'n':
  1634. ret.append('\n');
  1635. continue;
  1636. case 'r':
  1637. ret.append('\r');
  1638. break;
  1639. case 't':
  1640. ret.append('\t');
  1641. break;
  1642. default:
  1643. {
  1644. #ifdef JSONSTRICT
  1645. throw MakeStringException(-1, "invalid json escaped sequence");
  1646. #endif
  1647. ret.append('\\');
  1648. ret.append(*j);
  1649. break;
  1650. }
  1651. }
  1652. }
  1653. }
  1654. }
  1655. catch (IException *)
  1656. {
  1657. if (errMark) *errMark = j;
  1658. throw;
  1659. }
  1660. return j;
  1661. }
  1662. void decodeXML(ISimpleReadStream &in, StringBuffer &out, unsigned len)
  1663. {
  1664. // TODO
  1665. UNIMPLEMENTED;
  1666. }
  1667. const char *decodeXML(const char *x, StringBuffer &ret, const char **errMark, IEntityHelper *entityHelper, bool strict)
  1668. {
  1669. if (!x)
  1670. return x;
  1671. try
  1672. {
  1673. while (*x)
  1674. {
  1675. if ('&' == *x)
  1676. {
  1677. switch (x[1])
  1678. {
  1679. case 'a':
  1680. switch (x[2])
  1681. {
  1682. case 'm':
  1683. {
  1684. if ('p' == x[3] && ';' == x[4])
  1685. {
  1686. x += 5;
  1687. ret.append('&');
  1688. continue;
  1689. }
  1690. break;
  1691. }
  1692. case 'p':
  1693. {
  1694. if ('o' == x[3] && 's' == x[4] && ';' == x[5])
  1695. {
  1696. x += 6;
  1697. ret.append('\'');
  1698. continue;
  1699. }
  1700. break;
  1701. }
  1702. }
  1703. break;
  1704. case 'l':
  1705. if ('t' == x[2] && ';' == x[3])
  1706. {
  1707. x += 4;
  1708. ret.append('<');
  1709. continue;
  1710. }
  1711. break;
  1712. case 'g':
  1713. if ('t' == x[2] && ';' == x[3])
  1714. {
  1715. x += 4;
  1716. ret.append('>');
  1717. continue;
  1718. }
  1719. break;
  1720. case 'q':
  1721. if ('u' == x[2] && 'o' == x[3] && 't' == x[4] && ';' == x[5])
  1722. {
  1723. x += 6;
  1724. ret.append('"');
  1725. continue;
  1726. }
  1727. break;
  1728. case 'n':
  1729. if ('b' == x[2] && 's' == x[3] && 'p' == x[4] && ';' == x[5])
  1730. {
  1731. x += 6;
  1732. writeUtf8(0xa0, ret);
  1733. continue;
  1734. }
  1735. break;
  1736. case '#':
  1737. {
  1738. const char *numstart = x+2;
  1739. int base = 10;
  1740. if (*numstart == 'x')
  1741. {
  1742. base = 16;
  1743. numstart++;
  1744. }
  1745. char *numend;
  1746. unsigned val = strtoul(numstart, &numend, base);
  1747. if (numstart==numend || *numend != ';')
  1748. {
  1749. if (strict)
  1750. throw MakeStringException(-1, "invalid escaped sequence");
  1751. }
  1752. else // always convert to utf-8. Should potentially throw error if not marked as utf-8 encoded doc and out of ascii range.
  1753. {
  1754. writeUtf8(val, ret);
  1755. x = numend+1;
  1756. continue;
  1757. }
  1758. break;
  1759. }
  1760. case ';':
  1761. case '\0':
  1762. if (strict)
  1763. throw MakeStringException(-1, "invalid escaped sequence");
  1764. break;
  1765. default:
  1766. if (entityHelper)
  1767. {
  1768. bool error = false;
  1769. const char *start=x+1;
  1770. const char *finger=start;
  1771. while (*finger && *finger != ';')
  1772. ++finger;
  1773. if (*finger == ';')
  1774. {
  1775. StringBuffer entity(finger-start, start);
  1776. if (entityHelper->find(entity, ret))
  1777. {
  1778. x = finger + 1;
  1779. continue;
  1780. }
  1781. }
  1782. }
  1783. if (strict)
  1784. throw MakeStringException(-1, "invalid escaped sequence");
  1785. break;
  1786. }
  1787. }
  1788. ret.append(*x);
  1789. ++x;
  1790. }
  1791. }
  1792. catch (IException *)
  1793. {
  1794. if (errMark) *errMark = x;
  1795. throw;
  1796. }
  1797. return x;
  1798. }
  1799. StringBuffer & appendXMLOpenTag(StringBuffer &xml, const char *tag, const char *prefix, bool complete, bool close, const char *uri)
  1800. {
  1801. if (!tag || !*tag)
  1802. return xml;
  1803. xml.append('<');
  1804. appendXMLTagName(xml, tag, prefix);
  1805. if (uri && *uri)
  1806. {
  1807. xml.append(" xmlns");
  1808. if (prefix && *prefix)
  1809. xml.append(':').append(prefix);
  1810. xml.append("=\"").append(uri).append('\"');
  1811. }
  1812. if (complete)
  1813. {
  1814. if (close)
  1815. xml.append('/');
  1816. xml.append('>');
  1817. }
  1818. return xml;
  1819. }
  1820. jlib_decl StringBuffer &appendJSONName(StringBuffer &s, const char *name)
  1821. {
  1822. if (!name || !*name)
  1823. return s;
  1824. delimitJSON(s);
  1825. return encodeJSON(s.append('"'), name).append("\": ");
  1826. }
  1827. jlib_decl StringBuffer &appendfJSONName(StringBuffer &s, const char *format, ...)
  1828. {
  1829. va_list args;
  1830. va_start(args, format);
  1831. StringBuffer vs;
  1832. vs.valist_appendf(format, args);
  1833. va_end(args);
  1834. return appendJSONName(s, vs);
  1835. }
  1836. static char hexchar[] = "0123456789ABCDEF";
  1837. jlib_decl StringBuffer &appendJSONDataValue(StringBuffer& s, const char *name, unsigned len, const void *_value)
  1838. {
  1839. appendJSONNameOrDelimit(s, name);
  1840. s.append('"');
  1841. const unsigned char *value = (const unsigned char *) _value;
  1842. for (unsigned int i = 0; i < len; i++)
  1843. s.append(hexchar[value[i] >> 4]).append(hexchar[value[i] & 0x0f]);
  1844. return s.append('"');
  1845. }
  1846. StringBuffer &appendJSONRealValue(StringBuffer& s, const char *name, double value)
  1847. {
  1848. appendJSONNameOrDelimit(s, name);
  1849. bool quoted = j_isnan(value) || j_isinf(value);
  1850. if (quoted)
  1851. s.append('"');
  1852. s.append(value);
  1853. if (quoted)
  1854. s.append('"');
  1855. return s;
  1856. }
  1857. inline StringBuffer &encodeJSONChar(StringBuffer &s, const char *&ch, unsigned &remaining)
  1858. {
  1859. byte next = *ch;
  1860. switch (next)
  1861. {
  1862. case '\b':
  1863. s.append("\\b");
  1864. break;
  1865. case '\f':
  1866. s.append("\\f");
  1867. break;
  1868. case '\n':
  1869. s.append("\\n");
  1870. break;
  1871. case '\r':
  1872. s.append("\\r");
  1873. break;
  1874. case '\t':
  1875. s.append("\\t");
  1876. break;
  1877. case '\"':
  1878. case '\\':
  1879. case '/':
  1880. s.append('\\');
  1881. s.append(next);
  1882. break;
  1883. case '\0':
  1884. s.append("\\u0000");
  1885. break;
  1886. default:
  1887. if (next >= ' ' && next < 128)
  1888. s.append(next);
  1889. else if (next < ' ' && next > 0)
  1890. s.append("\\u00").appendhex(next, true);
  1891. else //json is always supposed to be utf8 (or other unicode formats)
  1892. {
  1893. unsigned chlen = utf8CharLen((const unsigned char *)ch, remaining);
  1894. if (chlen==0)
  1895. s.append("\\u00").appendhex(next, true);
  1896. else
  1897. {
  1898. s.append(chlen, ch);
  1899. ch += (chlen-1);
  1900. remaining -= (chlen-1);
  1901. }
  1902. }
  1903. break;
  1904. }
  1905. ch++;
  1906. remaining--;
  1907. return s;
  1908. }
  1909. StringBuffer &encodeJSON(StringBuffer &s, unsigned size, const char *value)
  1910. {
  1911. if (!value)
  1912. return s;
  1913. while (size)
  1914. encodeJSONChar(s, value, size);
  1915. return s;
  1916. }
  1917. StringBuffer &encodeJSON(StringBuffer &s, const char *value)
  1918. {
  1919. if (!value)
  1920. return s;
  1921. return encodeJSON(s, strlen(value), value);
  1922. }
  1923. void decodeCppEscapeSequence(StringBuffer & out, const char * in, bool errorIfInvalid)
  1924. {
  1925. out.ensureCapacity((size32_t)strlen(in));
  1926. while (*in)
  1927. {
  1928. char c = *in++;
  1929. if (c == '\\')
  1930. {
  1931. char next = *in;
  1932. if (next)
  1933. {
  1934. in++;
  1935. switch (next)
  1936. {
  1937. case 'a': c = '\a'; break;
  1938. case 'b': c = '\b'; break;
  1939. case 'f': c = '\f'; break;
  1940. case 'n': c = '\n'; break;
  1941. case 'r': c = '\r'; break;
  1942. case 't': c = '\t'; break;
  1943. case 'v': c = '\v'; break;
  1944. case '\\':
  1945. case '\'':
  1946. case '?':
  1947. case '\"': break;
  1948. case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7':
  1949. {
  1950. c = next - '0';
  1951. if (*in >= '0' && *in <= '7')
  1952. {
  1953. c = c << 3 | (*in++-'0');
  1954. if (*in >= '0' && *in <= '7')
  1955. c = c << 3 | (*in++-'0');
  1956. }
  1957. break;
  1958. }
  1959. case 'x':
  1960. c = 0;
  1961. while (isxdigit(*in))
  1962. {
  1963. next = *in++;
  1964. c = c << 4;
  1965. if (next >= '0' && next <= '9') c |= (next - '0');
  1966. else if (next >= 'A' && next <= 'F') c |= (next - 'A' + 10);
  1967. else if (next >= 'a' && next <= 'f') c |= (next - 'a' + 10);
  1968. }
  1969. break;
  1970. default:
  1971. if (errorIfInvalid)
  1972. throw MakeStringException(1, "unrecognised character escape sequence '\\%c'", next);
  1973. in--; // keep it as is.
  1974. break;
  1975. }
  1976. }
  1977. }
  1978. out.append(c);
  1979. }
  1980. }
  1981. bool isPrintable(unsigned len, const char * src)
  1982. {
  1983. while (len--)
  1984. {
  1985. if (!isprint(*((unsigned char *)src)))
  1986. return false;
  1987. src++;
  1988. }
  1989. return true;
  1990. }
  1991. //make this as fast as possible...
  1992. StringBuffer & appendStringAsSQL(StringBuffer & out, unsigned len, const char * src)
  1993. {
  1994. if (!isPrintable(len, src))
  1995. {
  1996. out.append("X'");
  1997. appendDataAsHex(out, len, src);
  1998. return out.append('\'');
  1999. }
  2000. out.ensureCapacity(2 + len);
  2001. out.append('\'');
  2002. loop
  2003. {
  2004. char * next = (char *)memchr(src, '\'', len);
  2005. if (!next)
  2006. break;
  2007. unsigned chunk=(size32_t)(next-src)+1;
  2008. out.append(chunk, src).append('\'');
  2009. len -= chunk;
  2010. src += chunk;
  2011. }
  2012. return out.append(len, src).append('\'');
  2013. }
  2014. static const char * hexText = "0123456789ABCDEF";
  2015. StringBuffer & appendDataAsHex(StringBuffer &out, unsigned len, const void * data)
  2016. {
  2017. char * target = (char *)out.reserve(len*2);
  2018. unsigned char * start = (unsigned char *)data;
  2019. for (unsigned count=len; count> 0; --count)
  2020. {
  2021. unsigned next = *start++;
  2022. *target++ = hexText[next >>4];
  2023. *target++ = hexText[next & 15];
  2024. }
  2025. return out;
  2026. }
  2027. bool strToBool(size_t len, const char * text)
  2028. {
  2029. switch (len)
  2030. {
  2031. case 4:
  2032. if (memicmp(text, "true", 4) == 0)
  2033. return true;
  2034. break;
  2035. case 3:
  2036. if (memicmp(text, "yes", 3) == 0)
  2037. return true;
  2038. break;
  2039. case 2:
  2040. if (memicmp(text, "on", 2) == 0)
  2041. return true;
  2042. break;
  2043. case 1:
  2044. if ((memicmp(text, "t", 1) == 0) || (memicmp(text, "y", 1) == 0))
  2045. return true;
  2046. break;
  2047. }
  2048. while (len && isspace(*text))
  2049. {
  2050. len--;
  2051. text++;
  2052. }
  2053. while (len-- && isdigit(*text))
  2054. {
  2055. if (*text++ != '0') return true;
  2056. }
  2057. return false;
  2058. }
  2059. bool strToBool(const char * text)
  2060. {
  2061. return strToBool(strlen(text), text);
  2062. }
  2063. bool clipStrToBool(size_t len, const char * text)
  2064. {
  2065. while (len && *text==' ')
  2066. {
  2067. len--;
  2068. text++;
  2069. }
  2070. while (len && text[len-1]== ' ')
  2071. len--;
  2072. return strToBool(len, text);
  2073. }
  2074. bool clipStrToBool(const char * text)
  2075. {
  2076. return clipStrToBool(strlen(text), text);
  2077. }
  2078. StringBuffer & ncnameEscape(char const * in, StringBuffer & out)
  2079. {
  2080. if(!isalpha(*in))
  2081. {
  2082. out.appendf("_%02X", static_cast<unsigned char>(*in));
  2083. in++;
  2084. }
  2085. char const * finger = in;
  2086. while(*finger)
  2087. {
  2088. if(!isalnum(*finger))
  2089. {
  2090. if(finger>in)
  2091. out.append((size32_t)(finger-in), in);
  2092. out.appendf("_%02X", static_cast<unsigned char>(*finger));
  2093. in = ++finger;
  2094. }
  2095. else
  2096. {
  2097. finger++;
  2098. }
  2099. }
  2100. if(finger>in)
  2101. out.append((size32_t)(finger-in), in);
  2102. return out;
  2103. }
  2104. StringBuffer & ncnameUnescape(char const * in, StringBuffer & out)
  2105. {
  2106. char const * finger = in;
  2107. while(*finger)
  2108. {
  2109. if(*finger == '_')
  2110. {
  2111. if(finger>in)
  2112. out.append((size32_t)(finger-in), in);
  2113. unsigned char chr = 16 * hex2num(finger[1]) + hex2num(finger[2]);
  2114. out.append(static_cast<char>(chr));
  2115. in = (finger+=3);
  2116. }
  2117. else
  2118. {
  2119. finger++;
  2120. }
  2121. }
  2122. if(finger>in)
  2123. out.append((size32_t)(finger-in), in);
  2124. return out;
  2125. }
  2126. bool startsWith(const char* src, const char* dst)
  2127. {
  2128. while (*dst && *dst == *src) { src++; dst++; }
  2129. return *dst==0;
  2130. }
  2131. bool startsWithIgnoreCase(const char* src, const char* dst)
  2132. {
  2133. while (*dst && tolower(*dst) == tolower(*src)) { src++; dst++; }
  2134. return *dst==0;
  2135. }
  2136. bool endsWith(const char* src, const char* dst)
  2137. {
  2138. size_t srcLen = strlen(src);
  2139. size_t dstLen = strlen(dst);
  2140. if (dstLen<=srcLen)
  2141. return memcmp(dst, src+srcLen-dstLen, dstLen)==0;
  2142. return false;
  2143. }
  2144. bool endsWithIgnoreCase(const char* src, const char* dst)
  2145. {
  2146. size_t srcLen = strlen(src);
  2147. size_t dstLen = strlen(dst);
  2148. if (dstLen<=srcLen)
  2149. return memicmp(dst, src+srcLen-dstLen, dstLen)==0;
  2150. return false;
  2151. }
  2152. char *j_strtok_r(char *str, const char *delim, char **saveptr)
  2153. {
  2154. if (!str)
  2155. str = *saveptr;
  2156. char c;
  2157. loop {
  2158. c = *str;
  2159. if (!c) {
  2160. *saveptr = str;
  2161. return NULL;
  2162. }
  2163. if (!strchr(delim,c))
  2164. break;
  2165. str++;
  2166. }
  2167. char *ret=str;
  2168. do {
  2169. c = *(++str);
  2170. } while (c&&!strchr(delim,c));
  2171. if (c)
  2172. *(str++) = 0;
  2173. *saveptr = str;
  2174. return ret;
  2175. }
  2176. int j_memicmp (const void *s1, const void *s2, size32_t len)
  2177. {
  2178. const byte *b1 = (const byte *)s1;
  2179. const byte *b2 = (const byte *)s2;
  2180. int ret = 0;
  2181. while (len&&((ret = tolower(*b1)-tolower(*b2)) == 0)) {
  2182. b1++;
  2183. b2++;
  2184. len--;
  2185. }
  2186. return ret;
  2187. }
  2188. size32_t memcount(size32_t len, const char * str, char search)
  2189. {
  2190. size32_t count = 0;
  2191. for (size32_t i=0; i < len; i++)
  2192. {
  2193. if (str[i] == search)
  2194. count++;
  2195. }
  2196. return count;
  2197. }
  2198. StringBuffer & elideString(StringBuffer & s, unsigned maxLength)
  2199. {
  2200. if (s.length() > maxLength)
  2201. {
  2202. s.setLength(maxLength);
  2203. s.append("...");
  2204. }
  2205. return s;
  2206. }
  2207. const char * nullText(const char * text)
  2208. {
  2209. if (text) return text;
  2210. return "(null)";
  2211. }