jstring.cpp 59 KB

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