forked from orafce/orafce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plvstr.c
1342 lines (1121 loc) · 27.1 KB
/
plvstr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This code implements one part of functonality of
free available library PL/Vision. Please look www.quest.com
Original author: Steven Feuerstein, 1996 - 2002
PostgreSQL implementation author: Pavel Stehule, 2006
This module is under BSD Licence
History:
1.0. first public version 13. March 2006
*/
#include "postgres.h"
#include "utils/builtins.h"
#include "utils/numeric.h"
#include "string.h"
#include "stdlib.h"
#include "utils/pg_locale.h"
#include "mb/pg_wchar.h"
#include "nodes/execnodes.h"
#include "catalog/pg_type.h"
#include "libpq/pqformat.h"
#include "orafunc.h"
#include "builtins.h"
PG_FUNCTION_INFO_V1(plvstr_rvrs);
PG_FUNCTION_INFO_V1(plvstr_normalize);
PG_FUNCTION_INFO_V1(plvstr_is_prefix);
PG_FUNCTION_INFO_V1(plvstr_is_prefix_text);
PG_FUNCTION_INFO_V1(plvstr_is_prefix_int);
PG_FUNCTION_INFO_V1(plvstr_is_prefix_int64);
PG_FUNCTION_INFO_V1(plvstr_lpart);
PG_FUNCTION_INFO_V1(plvstr_rpart);
PG_FUNCTION_INFO_V1(plvstr_lstrip);
PG_FUNCTION_INFO_V1(plvstr_rstrip);
PG_FUNCTION_INFO_V1(plvstr_left);
PG_FUNCTION_INFO_V1(plvstr_right);
PG_FUNCTION_INFO_V1(plvstr_substr2);
PG_FUNCTION_INFO_V1(plvstr_substr3);
PG_FUNCTION_INFO_V1(plvstr_instr2);
PG_FUNCTION_INFO_V1(plvstr_instr3);
PG_FUNCTION_INFO_V1(plvstr_instr4);
PG_FUNCTION_INFO_V1(plvstr_betwn_i);
PG_FUNCTION_INFO_V1(plvstr_betwn_c);
PG_FUNCTION_INFO_V1(plvstr_swap);
PG_FUNCTION_INFO_V1(plvchr_nth);
PG_FUNCTION_INFO_V1(plvchr_first);
PG_FUNCTION_INFO_V1(plvchr_last);
PG_FUNCTION_INFO_V1(plvchr_is_kind_i);
PG_FUNCTION_INFO_V1(plvchr_is_kind_a);
PG_FUNCTION_INFO_V1(plvchr_char_name);
PG_FUNCTION_INFO_V1(oracle_substr2);
PG_FUNCTION_INFO_V1(oracle_substr3);
static text *ora_substr(Datum str, int start, int len);
#define ora_substr_text(str, start, len) \
ora_substr(PointerGetDatum((str)), (start), (len))
static const char* char_names[] = {
"NULL","SOH","STX","ETX","EOT","ENQ","ACK","DEL",
"BS", "HT", "NL", "VT", "NP", "CR", "SO", "SI",
"DLE", "DC1","DC2","DC3","DC4","NAK","SYN","ETB",
"CAN", "EM","SUB","ESC","FS","GS","RS","US","SP"
};
#define NON_EMPTY_CHECK(str) \
if (VARSIZE_ANY_EXHDR(str) == 0) \
ereport(ERROR, \
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), \
errmsg("invalid parameter"), \
errdetail("Not allowed empty string.")));
#define PARAMETER_ERROR(detail) \
ereport(ERROR, \
(errcode(ERRCODE_INVALID_PARAMETER_VALUE), \
errmsg("invalid parameter"), \
errdetail(detail)));
#ifndef _pg_mblen
#define _pg_mblen pg_mblen
#endif
typedef enum
{
POSITION,
FIRST,
LAST
} position_mode;
#if PG_VERSION_NUM < 80400
text *
cstring_to_text_with_len(const char *c, int n)
{
text *result;
result = palloc(n + VARHDRSZ);
SET_VARSIZE(result, n + VARHDRSZ);
memcpy(VARDATA(result), c, n);
return result;
}
#endif
/*
* Make substring, can handle negative start
*
*/
int
ora_mb_strlen(text *str, char **sizes, int **positions)
{
int r_len;
int cur_size = 0;
int sz;
char *p;
int cur = 0;
p = VARDATA_ANY(str);
r_len = VARSIZE_ANY_EXHDR(str);
if (NULL != sizes)
*sizes = palloc(r_len * sizeof(char));
if (NULL != positions)
*positions = palloc(r_len * sizeof(int));
while (cur < r_len)
{
sz = _pg_mblen(p);
if (sizes)
(*sizes)[cur_size] = sz;
if (positions)
(*positions)[cur_size] = cur;
cur += sz;
p += sz;
cur_size += 1;
}
return cur_size;
}
int
ora_mb_strlen1(text *str)
{
int r_len;
int c;
char *p;
r_len = VARSIZE_ANY_EXHDR(str);
if (pg_database_encoding_max_length() == 1)
return r_len;
p = VARDATA_ANY(str);
c = 0;
while (r_len > 0)
{
int sz;
sz = _pg_mblen(p);
p += sz;
r_len -= sz;
c += 1;
}
return c;
}
/*
* len < 0 means "length is not specified".
*/
static text *
ora_substr(Datum str, int start, int len)
{
if (start == 0)
start = 1; /* 0 is interpreted as 1 */
else if (start < 0)
{
text *t;
int32 n;
t = DatumGetTextPP(str);
n = pg_mbstrlen_with_len(VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t));
start = n + start + 1;
if (start <= 0)
return cstring_to_text("");
str = PointerGetDatum(t); /* save detoasted text */
}
if (len < 0)
return DatumGetTextP(DirectFunctionCall2(text_substr_no_len,
str, Int32GetDatum(start)));
else
return DatumGetTextP(DirectFunctionCall3(text_substr,
str, Int32GetDatum(start), Int32GetDatum(len)));
}
/* simply search algorhitm - can be better */
static int
ora_instr_mb(text *txt, text *pattern, int start, int nth)
{
int c_len_txt, c_len_pat;
int b_len_pat;
int *pos_txt;
const char *str_txt, *str_pat;
int beg, end, i, dx;
str_txt = VARDATA_ANY(txt);
c_len_txt = ora_mb_strlen(txt, NULL, &pos_txt);
str_pat = VARDATA_ANY(pattern);
b_len_pat = VARSIZE_ANY_EXHDR(pattern);
c_len_pat = pg_mbstrlen_with_len(str_pat, b_len_pat);
if (start > 0)
{
dx = 1;
beg = start - 1;
end = c_len_txt - c_len_pat + 1;
if (beg >= end)
return 0; /* out of range */
}
else
{
dx = -1;
beg = Min(c_len_txt + start, c_len_txt - c_len_pat);
end = -1;
if (beg <= end)
return 0; /* out of range */
}
for (i = beg; i != end; i += dx)
{
if (memcmp(str_txt + pos_txt[i], str_pat, b_len_pat) == 0)
{
if (--nth == 0)
return i + 1;
}
}
return 0;
}
int
ora_instr(text *txt, text *pattern, int start, int nth)
{
int len_txt, len_pat;
const char *str_txt, *str_pat;
int beg, end, i, dx;
if (nth <= 0)
PARAMETER_ERROR("Four parameter isn't positive.");
/* Forward for multibyte strings */
if (pg_database_encoding_max_length() > 1)
return ora_instr_mb(txt, pattern, start, nth);
str_txt = VARDATA_ANY(txt);
len_txt = VARSIZE_ANY_EXHDR(txt);
str_pat = VARDATA_ANY(pattern);
len_pat = VARSIZE_ANY_EXHDR(pattern);
if (start > 0)
{
dx = 1;
beg = start - 1;
end = len_txt - len_pat + 1;
if (beg >= end)
return 0; /* out of range */
}
else
{
dx = -1;
beg = Min(len_txt + start, len_txt - len_pat);
end = -1;
if (beg <= end)
return 0; /* out of range */
}
for (i = beg; i != end; i += dx)
{
if (memcmp(str_txt + i, str_pat, len_pat) == 0)
{
if (--nth == 0)
return i + 1;
}
}
return 0;
}
/****************************************************************
* PLVstr.normalize
*
* Syntax:
* FUNCTION plvstr.normalize (string_in IN VARCHAR)
* RETURN VARCHAR;
*
* Purpouse:
* Normalize string - replace white chars by space, replace
* spaces by space
*
****************************************************************/
Datum
plvstr_normalize(PG_FUNCTION_ARGS)
{
text *str = PG_GETARG_TEXT_PP(0);
text *result;
char *aux, *aux_cur;
int i, l;
char c, *cur;
bool write_spc = false;
bool ignore_stsp = true;
bool mb_encode;
int sz;
mb_encode = pg_database_encoding_max_length() > 1;
l = VARSIZE_ANY_EXHDR(str);
aux_cur = aux = palloc(l);
write_spc = false;
cur = VARDATA_ANY(str);
for (i = 0; i < l; i++)
{
switch ((c = *cur))
{
case '\t':
case '\n':
case '\r':
case ' ':
write_spc = ignore_stsp ? false : true;
break;
default:
/* ignore all other unvisible chars */
if (mb_encode)
{
sz = _pg_mblen(cur);
if (sz > 1 || (sz == 1 && c > 32))
{
int j;
if (write_spc)
{
*aux_cur++ = ' ';
write_spc = false;
}
for (j = 0; j < sz; j++)
{
*aux_cur++ = *cur++;
}
ignore_stsp = false;
i += sz - 1;
}
continue;
}
else
if (c > 32)
{
if (write_spc)
{
*aux_cur++ = ' ';
write_spc = false;
}
*aux_cur++ = c;
ignore_stsp = false;
continue;
}
}
cur += 1;
}
l = aux_cur - aux;
result = palloc(l+VARHDRSZ);
SET_VARSIZE(result, l + VARHDRSZ);
memcpy(VARDATA(result), aux, l);
PG_RETURN_TEXT_P(result);
}
/****************************************************************
* PLVstr.instr
*
* Syntax:
* FUNCTION plvstr.instr (string_in VARCHAR, pattern VARCHAR)
* FUNCTION plvstr.instr (string_in VARCHAR, pattern VARCHAR,
* start_in INTEGER)
* FUNCTION plvstr.instr (string_in VARCHAR, pattern VARCHAR,
* start_in INTEGER, nth INTEGER)
* RETURN INT;
*
* Purpouse:
* Search pattern in string.
*
****************************************************************/
Datum
plvstr_instr2 (PG_FUNCTION_ARGS)
{
text *arg1 = PG_GETARG_TEXT_PP(0);
text *arg2 = PG_GETARG_TEXT_PP(1);
PG_RETURN_INT32(ora_instr(arg1, arg2, 1, 1));
}
Datum
plvstr_instr3 (PG_FUNCTION_ARGS)
{
text *arg1 = PG_GETARG_TEXT_PP(0);
text *arg2 = PG_GETARG_TEXT_PP(1);
int arg3 = PG_GETARG_INT32(2);
PG_RETURN_INT32(ora_instr(arg1, arg2, arg3, 1));
}
Datum
plvstr_instr4 (PG_FUNCTION_ARGS)
{
text *arg1 = PG_GETARG_TEXT_PP(0);
text *arg2 = PG_GETARG_TEXT_PP(1);
int arg3 = PG_GETARG_INT32(2);
int arg4 = PG_GETARG_INT32(3);
PG_RETURN_INT32(ora_instr(arg1, arg2, arg3, arg4));
}
/****************************************************************
* PLVstr.is_prefix
*
* Syntax:
* FUNCTION plvstr.is_prefix (string_in IN VARCHAR,
* prefix_in VARCHAR,
* case_sensitive BOOL := true)
* RETURN bool;
* FUNCTION plvstr.is_prefix (num_in IN NUMERIC,
* prefix_in NUMERIC) RETURN bool;
* FUNCTION plvstr.is_prefix (int_in IN INT,
* prefix_in INT) RETURN bool;
*
* Purpouse:
* Returns true, if prefix_in is prefix of string_in
*
****************************************************************/
Datum
plvstr_is_prefix_text (PG_FUNCTION_ARGS)
{
text *str = PG_GETARG_TEXT_PP(0);
text *prefix = PG_GETARG_TEXT_PP(1);
bool case_sens = PG_GETARG_BOOL(2);
bool mb_encode;
int str_len = VARSIZE_ANY_EXHDR(str);
int pref_len = VARSIZE_ANY_EXHDR(prefix);
int i;
char *ap, *bp;
mb_encode = pg_database_encoding_max_length() > 1;
if (mb_encode && !case_sens)
{
str = (text*)DatumGetPointer(DirectFunctionCall1(lower, PointerGetDatum(str)));
prefix = (text*)DatumGetPointer(DirectFunctionCall1(lower, PointerGetDatum(prefix)));
}
ap = VARDATA_ANY(str);
bp = VARDATA_ANY(prefix);
for (i = 0; i < pref_len; i++)
{
if (i >= str_len)
break;
if (case_sens || mb_encode)
{
if (*ap++ != *bp++)
break;
}
else if (!mb_encode)
{
if (pg_toupper((unsigned char) *ap++) != pg_toupper((unsigned char) *bp++))
break;
}
}
PG_RETURN_BOOL(i == pref_len);
}
Datum
plvstr_is_prefix_int (PG_FUNCTION_ARGS)
{
int n = PG_GETARG_INT32(0);
int prefix = PG_GETARG_INT32(1);
bool result = false;
do
{
if (n == prefix)
{
result = true;
break;
}
n = n / 10;
} while (n >= prefix);
PG_RETURN_BOOL(result);
}
Datum
plvstr_is_prefix_int64 (PG_FUNCTION_ARGS)
{
int64 n = PG_GETARG_INT64(0);
int64 prefix = PG_GETARG_INT64(1);
bool result = false;
do
{
if (n == prefix)
{
result = true;
break;
}
n = n / 10;
} while (n >= prefix);
PG_RETURN_BOOL(result);
}
/****************************************************************
* PLVstr.rvrs
*
* Syntax:
* FUNCTION plvstr.rvrs (string_in IN VARCHAR,
* start_in IN INTEGER := 1,
* end_in IN INTEGER := NULL)
* RETURN VARCHAR2;
*
* Purpouse:
* Reverse string or part of string
*
****************************************************************/
Datum
plvstr_rvrs(PG_FUNCTION_ARGS)
{
text *str = PG_GETARG_TEXT_PP(0);
int start = PG_GETARG_INT32(1);
int end = PG_GETARG_INT32(2);
int len;
int i;
int new_len;
text *result;
char *data;
char *sizes = NULL;
int *positions = NULL;
bool mb_encode;
if (PG_ARGISNULL(0))
PG_RETURN_NULL();
mb_encode = pg_database_encoding_max_length() > 1;
if (!mb_encode)
len = VARSIZE_ANY_EXHDR(str);
else
len = ora_mb_strlen(str, &sizes, &positions);
start = PG_ARGISNULL(1) ? 1 : start;
end = PG_ARGISNULL(2) ? (start < 0 ? -len : len) : end;
if ((start > end && start > 0) || (start < end && start < 0))
PARAMETER_ERROR("Second parameter is bigger than third.");
if (start < 0)
{
end = len + start + 1;
start = end;
}
new_len = end - start + 1;
if (mb_encode)
{
int max_size;
int cur_size;
char *p;
int j;
int fz_size;
fz_size = VARSIZE_ANY_EXHDR(str);
if ((max_size = (new_len*pg_database_encoding_max_length())) > fz_size)
result = palloc(fz_size + VARHDRSZ);
else
result = palloc(max_size + VARHDRSZ);
data = (char*) VARDATA(result);
cur_size = 0;
p = VARDATA_ANY(str);
for (i = end - 1; i>= start - 1; i--)
{
for (j=0; j<sizes[i]; j++)
*data++ = *(p+positions[i]+j);
cur_size += sizes[i];
}
SET_VARSIZE(result, cur_size + VARHDRSZ);
}
else
{
char *p = VARDATA_ANY(str);
result = palloc(new_len + VARHDRSZ);
data = (char*) VARDATA(result);
SET_VARSIZE(result, new_len + VARHDRSZ);
for (i = end - 1; i >= start - 1; i--)
*data++ = p[i];
}
PG_RETURN_TEXT_P(result);
}
/****************************************************************
* PLVstr.lpart
*
* Syntax:
* FUNCTION PLVstr.lpart (string_in IN VARCHAR,
* divider_in IN VARCHAR,
* start_in IN INTEGER := 1,
* nth_in IN INTEGER := 1,
* all_if_notfound_in IN BOOLEAN := FALSE)
* RETURN VARCHAR2;
*
* Purpouse:
* Call this function to return the left part of a string.
*
****************************************************************/
Datum
plvstr_lpart (PG_FUNCTION_ARGS)
{
text *str = PG_GETARG_TEXT_P(0);
text *div = PG_GETARG_TEXT_P(1);
int start = PG_GETARG_INT32(2);
int nth = PG_GETARG_INT32(3);
bool all_if_notfound = PG_GETARG_BOOL(4);
int loc;
loc = ora_instr(str, div, start, nth);
if (loc == 0)
{
if (all_if_notfound)
PG_RETURN_TEXT_P(TextPCopy(str));
else
PG_RETURN_NULL();
}
else
PG_RETURN_TEXT_P(ora_substr_text(str, 1, loc-1));
}
/****************************************************************
* PLVstr.rpart
*
* Syntax:
* FUNCTION PLVstr.rpart (string_in IN VARCHAR,
* divider_in IN VARCHAR,
* start_in IN INTEGER := 1,
* nth_in IN INTEGER := 1,
* all_if_notfound_in IN BOOLEAN := FALSE)
* RETURN VARCHAR2;
*
* Purpouse:
* Call this function to return the right part of a string.
*
****************************************************************/
Datum
plvstr_rpart (PG_FUNCTION_ARGS)
{
text *str = PG_GETARG_TEXT_P(0);
text *div = PG_GETARG_TEXT_P(1);
int start = PG_GETARG_INT32(2);
int nth = PG_GETARG_INT32(3);
bool all_if_notfound = PG_GETARG_BOOL(4);
int loc;
loc = ora_instr(str, div, start, nth);
if (loc == 0)
{
if (all_if_notfound)
PG_RETURN_TEXT_P(TextPCopy(str));
else
PG_RETURN_NULL();
}
else
PG_RETURN_TEXT_P(ora_substr_text(str, loc+1, -1));
}
/****************************************************************
* PLVstr.lstrip
*
* Syntax:
* FUNCTION plvstr.lstrip (string_in IN VARCHAR,
* substring_in IN VARCHAR,
* num_in IN INTEGER := 1)
* RETURN VARCHAR;
*
* Purpouse:
* Call this function to remove characters from the beginning
* (left) of a string.
*
****************************************************************/
Datum
plvstr_lstrip (PG_FUNCTION_ARGS)
{
text *str = PG_GETARG_TEXT_PP(0);
text *pat = PG_GETARG_TEXT_PP(1);
int num = PG_GETARG_INT32(2);
int count = 0;
int len_p, len_s, i;
char *str_p, *aux_str_p, *pat_p;
len_p = VARSIZE_ANY_EXHDR(pat);
len_s = VARSIZE_ANY_EXHDR(str);
str_p = VARDATA_ANY(str);
while (count < num)
{
pat_p = VARDATA_ANY(pat);
aux_str_p = str_p;
if (len_s < len_p)
break;
for (i = 0; i < len_p; i++)
if (*aux_str_p++ != *pat_p++)
break;
if (i >= len_p)
{
count++;
/* found */
str_p = aux_str_p;
len_s -= len_p;
continue;
}
break;
}
PG_RETURN_TEXT_P(cstring_to_text_with_len(str_p,len_s));
}
/****************************************************************
* PLVstr.rstrip
*
* Syntax:
* FUNCTION plvstr.rstrip (string_in IN VARCHAR,
* substring_in IN VARCHAR,
* num_in IN INTEGER := 1)
* RETURN VARCHAR;
*
* Purpouse:
* Call this function to remove characters from the end
* (right) of a string.
*
****************************************************************/
Datum
plvstr_rstrip (PG_FUNCTION_ARGS)
{
text *str = PG_GETARG_TEXT_PP(0);
text *pat = PG_GETARG_TEXT_PP(1);
int num = PG_GETARG_INT32(2);
int count = 0;
int len_p, len_s, i;
char *str_p, *aux_str_p, *pat_p;
len_p = VARSIZE_ANY_EXHDR(pat);
len_s = VARSIZE_ANY_EXHDR(str);
str_p = VARDATA_ANY(str) + len_s - 1;
while (count < num)
{
pat_p = VARDATA_ANY(pat) + len_p - 1;
aux_str_p = str_p;
if (len_s < len_p)
break;
for (i = 0; i < len_p; i++)
if (*aux_str_p-- != *pat_p--)
break;
if (i >= len_p)
{
count++;
/* found */
str_p = aux_str_p;
len_s -= len_p;
continue;
}
break;
}
PG_RETURN_TEXT_P(cstring_to_text_with_len(VARDATA_ANY(str),len_s));
}
/****************************************************************
* PLVstr.left
*
* Syntax:
* FUNCTION plvstr.left (string_in IN VARCHAR,
* num_in INTEGER)
* RETURN VARCHAR;
*
* Purpouse:
* Returns firs num_in charaters. You can use negative num_in
* left('abcde', -2) -> abc
*
****************************************************************/
Datum
plvstr_left (PG_FUNCTION_ARGS)
{
text *str = PG_GETARG_TEXT_P(0);
int n = PG_GETARG_INT32(1);
if (n < 0)
n = ora_mb_strlen1(str) + n;
n = n < 0 ? 0 : n;
PG_RETURN_TEXT_P(ora_substr_text(str, 1, n));
}
/****************************************************************
* PLVstr.right
*
* Syntax:
* FUNCTION plvstr.right (string_in IN VARCHAR,
* num_in INTEGER)
* RETURN VARCHAR;
*
* Purpouse:
* Returns last (right) num_in characters.
*
****************************************************************/
Datum
plvstr_right (PG_FUNCTION_ARGS)
{
text *str = PG_GETARG_TEXT_P(0);
int n = PG_GETARG_INT32(1);
if (n < 0)
n = ora_mb_strlen1(str) + n;
n = (n < 0) ? 0 : n;
PG_RETURN_TEXT_P(ora_substr_text(str, -n, -1));
}
/****************************************************************
* PLVstr.substr2
*
* Syntax:
* FUNCTION plvstr.substr (string_in IN VARCHAR,
* start INTEGER)
* RETURN VARCHAR;
*
* Purpouse:
* Returns substring started on start_in to end
*
****************************************************************/
Datum
plvstr_substr2 (PG_FUNCTION_ARGS)
{
return oracle_substr2(fcinfo);
}
/****************************************************************
* PLVstr.substr3
*
* Syntax:
* FUNCTION plvstr.substr (string_in IN VARCHAR,
* start INTEGER, len INTEGER)
* RETURN VARCHAR;
*
* Purpouse:
* Returns len chars from start_in position
*
****************************************************************/
Datum
plvstr_substr3 (PG_FUNCTION_ARGS)
{
return oracle_substr3(fcinfo);
}
/****************************************************************
* PLVchr.nth
*
* Syntax:
* FUNCTION plvchr.nth (string_in IN VARCHAR,
* nth_in IN INTEGER)
* RETURN VARCHAR;
*
* Purpouse:
* Call this function to return the Nth character in a string.
*
****************************************************************/
Datum
plvchr_nth (PG_FUNCTION_ARGS)
{
PG_RETURN_TEXT_P(ora_substr(PG_GETARG_DATUM(0), PG_GETARG_INT32(1), 1));
}
/****************************************************************
* PLVchr.first
*
* Syntax:
* FUNCTION plvchr.first (string_in IN VARCHAR,
* RETURN VARCHAR;
*
* Purpouse:
* Call this function to return the first character in a string.
*
****************************************************************/
Datum
plvchr_first (PG_FUNCTION_ARGS)
{
PG_RETURN_TEXT_P(ora_substr(PG_GETARG_DATUM(0), 1, 1));
}
/****************************************************************
* PLVchr.last
*
* Syntax:
* FUNCTION plvchr.last (string_in IN VARCHAR,
* RETURN VARCHAR;
*
* Purpouse:
* Call this function to return the last character in a string.
*
****************************************************************/
Datum
plvchr_last (PG_FUNCTION_ARGS)
{
PG_RETURN_TEXT_P(ora_substr(PG_GETARG_DATUM(0), -1, 1));
}
/****************************************************************
* PLVchr.is_blank, plvchr.is_digit, ...
*
* Syntax:
* FUNCTION plvchr.is_kind (string_in IN VARCHAR,
* kind INT)
* RETURN VARCHAR;
*
* Purpouse:
* Call this function to see if a character is blank, ...