24
24
static text * dotrim (const char * string , int stringlen ,
25
25
const char * set , int setlen ,
26
26
bool doltrim , bool dortrim );
27
+ static bytea * dobyteatrim (bytea * string , bytea * set ,
28
+ bool doltrim , bool dortrim );
27
29
28
30
29
31
/********************************************************************
@@ -521,27 +523,12 @@ dotrim(const char *string, int stringlen,
521
523
return cstring_to_text_with_len (string , stringlen );
522
524
}
523
525
524
- /********************************************************************
525
- *
526
- * byteatrim
527
- *
528
- * Syntax:
529
- *
530
- * bytea byteatrim(bytea string, bytea set)
531
- *
532
- * Purpose:
533
- *
534
- * Returns string with characters removed from the front and back
535
- * up to the first character not in set.
536
- *
537
- * Cloned from btrim and modified as required.
538
- ********************************************************************/
539
-
540
- Datum
541
- byteatrim (PG_FUNCTION_ARGS )
526
+ /*
527
+ * Common implementation for bytea versions of btrim, ltrim, rtrim
528
+ */
529
+ bytea *
530
+ dobyteatrim (bytea * string , bytea * set , bool doltrim , bool dortrim )
542
531
{
543
- bytea * string = PG_GETARG_BYTEA_PP (0 );
544
- bytea * set = PG_GETARG_BYTEA_PP (1 );
545
532
bytea * ret ;
546
533
char * ptr ,
547
534
* end ,
@@ -556,47 +543,134 @@ byteatrim(PG_FUNCTION_ARGS)
556
543
setlen = VARSIZE_ANY_EXHDR (set );
557
544
558
545
if (stringlen <= 0 || setlen <= 0 )
559
- PG_RETURN_BYTEA_P ( string ) ;
546
+ return string ;
560
547
561
548
m = stringlen ;
562
549
ptr = VARDATA_ANY (string );
563
550
end = ptr + stringlen - 1 ;
564
551
ptr2start = VARDATA_ANY (set );
565
552
end2 = ptr2start + setlen - 1 ;
566
553
567
- while ( m > 0 )
554
+ if ( doltrim )
568
555
{
569
- ptr2 = ptr2start ;
570
- while (ptr2 <= end2 )
556
+ while (m > 0 )
571
557
{
572
- if (* ptr == * ptr2 )
558
+ ptr2 = ptr2start ;
559
+ while (ptr2 <= end2 )
560
+ {
561
+ if (* ptr == * ptr2 )
562
+ break ;
563
+ ++ ptr2 ;
564
+ }
565
+ if (ptr2 > end2 )
573
566
break ;
574
- ++ ptr2 ;
567
+ ptr ++ ;
568
+ m -- ;
575
569
}
576
- if (ptr2 > end2 )
577
- break ;
578
- ptr ++ ;
579
- m -- ;
580
570
}
581
571
582
- while ( m > 0 )
572
+ if ( dortrim )
583
573
{
584
- ptr2 = ptr2start ;
585
- while (ptr2 <= end2 )
574
+ while (m > 0 )
586
575
{
587
- if (* end == * ptr2 )
576
+ ptr2 = ptr2start ;
577
+ while (ptr2 <= end2 )
578
+ {
579
+ if (* end == * ptr2 )
580
+ break ;
581
+ ++ ptr2 ;
582
+ }
583
+ if (ptr2 > end2 )
588
584
break ;
589
- ++ ptr2 ;
585
+ end -- ;
586
+ m -- ;
590
587
}
591
- if (ptr2 > end2 )
592
- break ;
593
- end -- ;
594
- m -- ;
595
588
}
596
589
597
590
ret = (bytea * ) palloc (VARHDRSZ + m );
598
591
SET_VARSIZE (ret , VARHDRSZ + m );
599
592
memcpy (VARDATA (ret ), ptr , m );
593
+ return ret ;
594
+ }
595
+
596
+ /********************************************************************
597
+ *
598
+ * byteatrim
599
+ *
600
+ * Syntax:
601
+ *
602
+ * bytea byteatrim(bytea string, bytea set)
603
+ *
604
+ * Purpose:
605
+ *
606
+ * Returns string with characters removed from the front and back
607
+ * up to the first character not in set.
608
+ *
609
+ * Cloned from btrim and modified as required.
610
+ ********************************************************************/
611
+
612
+ Datum
613
+ byteatrim (PG_FUNCTION_ARGS )
614
+ {
615
+ bytea * string = PG_GETARG_BYTEA_PP (0 );
616
+ bytea * set = PG_GETARG_BYTEA_PP (1 );
617
+ bytea * ret ;
618
+
619
+ ret = dobyteatrim (string , set , true, true);
620
+
621
+ PG_RETURN_BYTEA_P (ret );
622
+ }
623
+
624
+ /********************************************************************
625
+ *
626
+ * bytealtrim
627
+ *
628
+ * Syntax:
629
+ *
630
+ * bytea bytealtrim(bytea string, bytea set)
631
+ *
632
+ * Purpose:
633
+ *
634
+ * Returns string with initial characters removed up to the first
635
+ * character not in set.
636
+ *
637
+ ********************************************************************/
638
+
639
+ Datum
640
+ bytealtrim (PG_FUNCTION_ARGS )
641
+ {
642
+ bytea * string = PG_GETARG_BYTEA_PP (0 );
643
+ bytea * set = PG_GETARG_BYTEA_PP (1 );
644
+ bytea * ret ;
645
+
646
+ ret = dobyteatrim (string , set , true, false);
647
+
648
+ PG_RETURN_BYTEA_P (ret );
649
+ }
650
+
651
+ /********************************************************************
652
+ *
653
+ * byteartrim
654
+ *
655
+ * Syntax:
656
+ *
657
+ * bytea byteartrim(bytea string, bytea set)
658
+ *
659
+ * Purpose:
660
+ *
661
+ * Returns string with final characters removed after the last
662
+ * character not in set.
663
+ *
664
+ ********************************************************************/
665
+
666
+ Datum
667
+ byteartrim (PG_FUNCTION_ARGS )
668
+ {
669
+ bytea * string = PG_GETARG_BYTEA_PP (0 );
670
+ bytea * set = PG_GETARG_BYTEA_PP (1 );
671
+ bytea * ret ;
672
+
673
+ ret = dobyteatrim (string , set , false, true);
600
674
601
675
PG_RETURN_BYTEA_P (ret );
602
676
}
0 commit comments