2
0

stringbuffer_glue.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. /*
  2. Copyright (c) 2016-2023 Bruce A Henderson
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include "brl.mod/blitz.mod/blitz.h"
  20. #ifndef BMX_NG
  21. extern unsigned short maxToLowerData[];
  22. extern unsigned short maxToUpperData[];
  23. #define bbToLowerData maxToLowerData
  24. #define bbToUpperData maxToUpperData
  25. #else
  26. #include "brl.mod/blitz.mod/blitz_unicode.h"
  27. #endif
  28. struct MaxStringBuffer {
  29. BBChar * buffer;
  30. int count;
  31. int capacity;
  32. };
  33. struct MaxSplitBuffer {
  34. struct MaxStringBuffer * buffer;
  35. int count;
  36. int * startIndex;
  37. int * endIndex;
  38. };
  39. void bmx_stringbuffer_free(struct MaxStringBuffer * buf) {
  40. free(buf->buffer);
  41. free(buf);
  42. }
  43. struct MaxStringBuffer * bmx_stringbuffer_new(int initial) {
  44. struct MaxStringBuffer * buf = malloc(sizeof(struct MaxStringBuffer));
  45. buf->count = 0;
  46. buf->capacity = initial;
  47. buf->buffer = malloc(initial * sizeof(BBChar));
  48. return buf;
  49. }
  50. /* make more capacity if requested size greater */
  51. void bmx_stringbuffer_resize(struct MaxStringBuffer * buf, int size) {
  52. if (buf->capacity < size) {
  53. if (buf->capacity * 2 > size) {
  54. size = buf->capacity * 2;
  55. }
  56. BBChar * newBuffer = (BBChar*)malloc(size * sizeof(BBChar));
  57. /* copy text to new buffer */
  58. memcpy(newBuffer, buf->buffer, buf->count * sizeof(BBChar));
  59. /* free old buffer */
  60. free(buf->buffer);
  61. buf->buffer = newBuffer;
  62. buf->capacity = size;
  63. }
  64. }
  65. int bmx_stringbuffer_count(struct MaxStringBuffer * buf) {
  66. return buf->count;
  67. }
  68. int bmx_stringbuffer_capacity(struct MaxStringBuffer * buf) {
  69. return buf->capacity;
  70. }
  71. void bmx_stringbuffer_setlength(struct MaxStringBuffer * buf, int length) {
  72. bmx_stringbuffer_resize(buf, length);
  73. if (length < buf->count) {
  74. buf->count = length;
  75. }
  76. }
  77. BBString * bmx_stringbuffer_tostring(struct MaxStringBuffer * buf) {
  78. if (!buf->count) {
  79. return &bbEmptyString;
  80. } else {
  81. return bbStringFromShorts(buf->buffer, buf->count);
  82. }
  83. }
  84. void bmx_stringbuffer_append_string(struct MaxStringBuffer * buf, BBString * value) {
  85. if (value != &bbEmptyString) {
  86. bmx_stringbuffer_resize(buf, buf->count + value->length);
  87. BBChar * p = buf->buffer + buf->count;
  88. memcpy(p, value->buf, value->length * sizeof(BBChar));
  89. buf->count += value->length;
  90. }
  91. }
  92. void bmx_stringbuffer_remove(struct MaxStringBuffer * buf, int start, int end) {
  93. if (start < 0 || start > buf->count || start > end) {
  94. return;
  95. }
  96. /* trim end if it is too big */
  97. if (end > buf->count) {
  98. end = buf->count;
  99. }
  100. /* still something to remove ? */
  101. if (buf->count - end != 0) {
  102. memcpy(buf->buffer + start, buf->buffer + end, (buf->count - end) * sizeof(BBChar));
  103. }
  104. buf->count -= end - start;
  105. }
  106. void bmx_stringbuffer_insert(struct MaxStringBuffer * buf, int offset, BBString * value) {
  107. if (value != &bbEmptyString) {
  108. if (offset < 0 || offset > buf->count) {
  109. return;
  110. }
  111. int length = value->length;
  112. bmx_stringbuffer_resize(buf, buf->count + length);
  113. /* make some space for the insertion */
  114. /* using memmove because data overlaps */
  115. memmove(buf->buffer + offset + length, buf->buffer + offset, (buf->count - offset) * sizeof(BBChar));
  116. /* insert the string */
  117. memcpy(buf->buffer + offset, value->buf, length * sizeof(BBChar));
  118. buf->count += length;
  119. }
  120. }
  121. void bmx_stringbuffer_reverse(struct MaxStringBuffer * buf) {
  122. int i = buf->count >> 1;
  123. int n = buf->count - i;
  124. while (--i >= 0) {
  125. BBChar c = buf->buffer[i];
  126. buf->buffer[i] = buf->buffer[n];
  127. buf->buffer[n] = c;
  128. n++;
  129. }
  130. }
  131. BBString * bmx_stringbuffer_substring(struct MaxStringBuffer * buf, int beginIndex, int endIndex) {
  132. if (!endIndex) {
  133. endIndex = buf->count;
  134. }
  135. if (beginIndex < 0 || endIndex > buf->count || endIndex < beginIndex) {
  136. return &bbEmptyString;
  137. }
  138. return bbStringFromShorts(buf->buffer + beginIndex, endIndex - beginIndex);
  139. }
  140. void bmx_stringbuffer_append_stringbuffer(struct MaxStringBuffer * buf, struct MaxStringBuffer * other) {
  141. if (other->count > 0) {
  142. bmx_stringbuffer_resize(buf, buf->count + other->count);
  143. memcpy(buf->buffer + buf->count, other->buffer, other->count * sizeof(BBChar));
  144. buf->count += other->count;
  145. }
  146. }
  147. int bmx_stringbuffer_matches(struct MaxStringBuffer * buf, int offset, BBString * subString) {
  148. int length = subString->length;
  149. int index = 0;
  150. while (--length >= 0) {
  151. if (buf->buffer[offset++] != subString->buf[index++]) {
  152. return 0;
  153. }
  154. }
  155. return 1;
  156. }
  157. int bmx_stringbuffer_startswith(struct MaxStringBuffer * buf, BBString * subString) {
  158. if (subString->length <= buf->count) {
  159. return bmx_stringbuffer_matches(buf, 0, subString);
  160. }
  161. return 0;
  162. }
  163. int bmx_stringbuffer_endswith(struct MaxStringBuffer * buf, BBString * subString) {
  164. if (subString->length <= buf->count) {
  165. return bmx_stringbuffer_matches(buf, buf->count - subString->length, subString);
  166. }
  167. return 0;
  168. }
  169. int bmx_stringbuffer_find(struct MaxStringBuffer * buf, BBString * subString, int startIndex) {
  170. if (startIndex < 0) {
  171. startIndex = 0;
  172. }
  173. int limit = buf->count - subString->length;
  174. while (startIndex <= limit) {
  175. if (bmx_stringbuffer_matches(buf, startIndex, subString)) {
  176. return startIndex;
  177. }
  178. startIndex++;
  179. }
  180. return -1;
  181. }
  182. int bmx_stringbuffer_findlast(struct MaxStringBuffer * buf, BBString * subString, int startIndex) {
  183. if (startIndex < 0) {
  184. startIndex = 0;
  185. }
  186. startIndex = buf->count - startIndex;
  187. if (startIndex + subString->length > buf->count) {
  188. startIndex = buf->count - subString->length;
  189. }
  190. while (startIndex >= 0) {
  191. if (bmx_stringbuffer_matches(buf, startIndex, subString)) {
  192. return startIndex;
  193. }
  194. startIndex--;
  195. }
  196. return -1;
  197. }
  198. void bmx_stringbuffer_tolower(struct MaxStringBuffer * buf) {
  199. int i;
  200. for (i = 0; i < buf->count; i++ ) {
  201. int c = buf->buffer[i];
  202. if (c < 192) {
  203. c = ( c >= 'A' && c <= 'Z') ? (c|32) : c;
  204. } else {
  205. int lo = 0, hi = 3828 / 4 - 1;
  206. while (lo <= hi) {
  207. int mid = (lo+hi)/2;
  208. if (c < bbToLowerData[mid*2]) {
  209. hi = mid-1;
  210. } else if (c > bbToLowerData[mid*2]) {
  211. lo = mid + 1;
  212. } else {
  213. c = bbToLowerData[mid*2+1];
  214. break;
  215. }
  216. }
  217. }
  218. buf->buffer[i]=c;
  219. }
  220. }
  221. void bmx_stringbuffer_toupper(struct MaxStringBuffer * buf) {
  222. int i;
  223. for (i = 0; i < buf->count; i++) {
  224. int c = buf->buffer[i];
  225. if (c < 181) {
  226. c = (c >= 'a' && c <= 'z') ? (c&~32) : c;
  227. } else {
  228. int lo = 0, hi = 3860/4-1;
  229. while (lo<=hi) {
  230. int mid=(lo+hi)/2;
  231. if (c < bbToUpperData[mid*2]) {
  232. hi = mid - 1;
  233. } else if (c > bbToUpperData[mid*2]) {
  234. lo = mid + 1;
  235. } else {
  236. c = bbToUpperData[mid*2+1];
  237. break;
  238. }
  239. }
  240. }
  241. buf->buffer[i]=c;
  242. }
  243. }
  244. void bmx_stringbuffer_trim(struct MaxStringBuffer * buf) {
  245. int start = 0;
  246. int end = buf->count;
  247. while (start < end && buf->buffer[start] <= ' ') {
  248. ++start;
  249. }
  250. if (start == end ) {
  251. buf->count = 0;
  252. return;
  253. }
  254. while (buf->buffer[end - 1] <= ' ') {
  255. --end;
  256. }
  257. if (end - start == buf->count) {
  258. return;
  259. }
  260. memmove(buf->buffer, buf->buffer + start, (end - start) * sizeof(BBChar));
  261. buf->count = end - start;
  262. }
  263. void bmx_stringbuffer_replace(struct MaxStringBuffer * buf, BBString * subString, BBString * withString) {
  264. if (!subString->length) {
  265. return;
  266. }
  267. struct MaxStringBuffer * newbuf = bmx_stringbuffer_new(16);
  268. int j, n;
  269. int i = 0;
  270. int p = 0;
  271. while( (j = bmx_stringbuffer_find(buf, subString, i)) != -1) {
  272. n = j - i;
  273. if (n) {
  274. bmx_stringbuffer_resize(newbuf, newbuf->count + n);
  275. memcpy(newbuf->buffer + p, buf->buffer + i, n * sizeof(BBChar));
  276. newbuf->count += n;
  277. p += n;
  278. }
  279. n = withString->length;
  280. bmx_stringbuffer_resize(newbuf, newbuf->count + n);
  281. memcpy(newbuf->buffer + p, withString->buf, n * sizeof(BBChar));
  282. newbuf->count += n;
  283. p += n;
  284. i = j + subString->length;
  285. }
  286. n = buf->count - i;
  287. if (n) {
  288. bmx_stringbuffer_resize(newbuf, newbuf->count + n);
  289. memcpy(newbuf->buffer + p, buf->buffer + i, n*sizeof(BBChar));
  290. newbuf->count += n;
  291. }
  292. bmx_stringbuffer_setlength(buf, 0);
  293. bmx_stringbuffer_append_stringbuffer(buf, newbuf);
  294. bmx_stringbuffer_free(newbuf);
  295. }
  296. void bmx_stringbuffer_join(struct MaxStringBuffer * buf, BBArray * bits, struct MaxStringBuffer * newbuf) {
  297. if(bits == &bbEmptyArray) {
  298. return;
  299. }
  300. int i;
  301. int n_bits = bits->scales[0];
  302. BBString **p = (BBString**)BBARRAYDATA( bits,1 );
  303. for(i = 0; i < n_bits; ++i) {
  304. if (i) {
  305. bmx_stringbuffer_append_stringbuffer(newbuf, buf);
  306. }
  307. BBString *bit = *p++;
  308. bmx_stringbuffer_append_string(newbuf, bit);
  309. }
  310. }
  311. struct MaxSplitBuffer * bmx_stringbuffer_split(struct MaxStringBuffer * buf, BBString * separator) {
  312. struct MaxSplitBuffer * splitBuffer = malloc(sizeof(struct MaxSplitBuffer));
  313. splitBuffer->buffer = buf;
  314. int count = 1;
  315. int i = 0;
  316. int offset = 0;
  317. if (separator->length > 0) {
  318. /* get a count of fields */
  319. while ((offset = bmx_stringbuffer_find(buf, separator, i)) != -1 ) {
  320. ++count;
  321. i = offset + separator->length;
  322. }
  323. splitBuffer->count = count;
  324. splitBuffer->startIndex = malloc(count * sizeof(int));
  325. splitBuffer->endIndex = malloc(count * sizeof(int));
  326. i = 0;
  327. int * bufferStartIndex = splitBuffer->startIndex;
  328. int * bufferEndIndex = splitBuffer->endIndex;
  329. while( count-- ){
  330. offset = bmx_stringbuffer_find(buf, separator, i);
  331. if (offset == -1) {
  332. offset = buf->count;
  333. }
  334. *bufferStartIndex++ = i;
  335. *bufferEndIndex++ = offset;
  336. i = offset + separator->length;
  337. }
  338. } else {
  339. // TODO - properly handle Null separator
  340. splitBuffer->count = count;
  341. splitBuffer->startIndex = malloc(count * sizeof(int));
  342. splitBuffer->endIndex = malloc(count * sizeof(int));
  343. *splitBuffer->startIndex = 0;
  344. *splitBuffer->endIndex = buf->count;
  345. }
  346. return splitBuffer;
  347. }
  348. void bmx_stringbuffer_setcharat(struct MaxStringBuffer * buf, int index, int ch) {
  349. if (index < 0 || index > buf->count) {
  350. return;
  351. }
  352. buf->buffer[index] = ch;
  353. }
  354. int bmx_stringbuffer_charat(struct MaxStringBuffer * buf, int index) {
  355. if (index < 0 || index > buf->count) {
  356. return 0;
  357. }
  358. return buf->buffer[index];
  359. }
  360. void bmx_stringbuffer_removecharat(struct MaxStringBuffer * buf, int index) {
  361. if (index < 0 || index >= buf->count) {
  362. return;
  363. }
  364. if (index < buf->count - 1) {
  365. memcpy(buf->buffer + index, buf->buffer + index + 1, (buf->count - index - 1) * sizeof(BBChar));
  366. }
  367. buf->count--;
  368. }
  369. void bmx_stringbuffer_append_cstring(struct MaxStringBuffer * buf, const char * chars) {
  370. int length = strlen(chars);
  371. if (length > 0) {
  372. int count = length;
  373. bmx_stringbuffer_resize(buf, buf->count + length);
  374. const char * p = chars;
  375. BBChar * b = buf->buffer + buf->count;
  376. while (length--) {
  377. *b++ = *p++;
  378. }
  379. buf->count += count;
  380. }
  381. }
  382. void bmx_stringbuffer_append_utf8string(struct MaxStringBuffer * buf, const char * chars) {
  383. int length = strlen(chars);
  384. if (length > 0) {
  385. int count = 0;
  386. bmx_stringbuffer_resize(buf, buf->count + length);
  387. int c;
  388. const char * p = chars;
  389. BBChar * b = buf->buffer + buf->count;
  390. while( (c=*p++ & 0xff) ){
  391. if( c<0x80 ){
  392. *b++=c;
  393. }else{
  394. int d=*p++ & 0x3f;
  395. if( c<0xe0 ){
  396. *b++=((c&31)<<6) | d;
  397. }else{
  398. int e=*p++ & 0x3f;
  399. if( c<0xf0 ){
  400. *b++=((c&15)<<12) | (d<<6) | e;
  401. }else{
  402. int f=*p++ & 0x3f;
  403. int v=((c&7)<<18) | (d<<12) | (e<<6) | f;
  404. if( v & 0xffff0000 ) bbExThrowCString( "Unicode character out of UCS-2 range" );
  405. *b++=v;
  406. }
  407. }
  408. }
  409. count++;
  410. }
  411. buf->count += count;
  412. }
  413. }
  414. void bmx_stringbuffer_append_shorts(struct MaxStringBuffer * buf, short * shorts, int length) {
  415. if (length > 0) {
  416. bmx_stringbuffer_resize(buf, buf->count + length);
  417. BBChar * p = buf->buffer + buf->count;
  418. memcpy(p, shorts, length * sizeof(BBChar));
  419. buf->count += length;
  420. }
  421. }
  422. void bmx_stringbuffer_append_char(struct MaxStringBuffer * buf, int value) {
  423. bmx_stringbuffer_resize(buf, buf->count + 1);
  424. BBChar * p = buf->buffer + buf->count;
  425. *p = (BBChar)value;
  426. buf->count++;
  427. }
  428. /* ----------------------------------------------------- */
  429. int bmx_stringbuffer_splitbuffer_length(struct MaxSplitBuffer * buf) {
  430. return buf->count;
  431. }
  432. BBString * bmx_stringbuffer_splitbuffer_text(struct MaxSplitBuffer * buf, int index) {
  433. if (index < 0 || index >= buf->count) {
  434. return &bbEmptyString;
  435. }
  436. return bmx_stringbuffer_substring(buf->buffer, buf->startIndex[index], buf->endIndex[index]);
  437. }
  438. void bmx_stringbuffer_splitbuffer_free(struct MaxSplitBuffer * buf) {
  439. free(buf->startIndex);
  440. free(buf->endIndex);
  441. free(buf);
  442. }
  443. BBArray * bmx_stringbuffer_splitbuffer_toarray(struct MaxSplitBuffer * buf) {
  444. int i, n;
  445. BBString **p,*bit;
  446. BBArray *bits;
  447. n = buf->count;
  448. bits = bbArrayNew1D("$", n);
  449. p = (BBString**)BBARRAYDATA(bits, 1);
  450. i = 0;
  451. while (n--) {
  452. bit = bmx_stringbuffer_substring(buf->buffer, buf->startIndex[i], buf->endIndex[i]);
  453. BBINCREFS( bit );
  454. *p++ = bit;
  455. i++;
  456. }
  457. return bits;
  458. }