2424static text * dotrim (const char * string , int stringlen ,
2525 const char * set , int setlen ,
2626 bool doltrim , bool dortrim );
27+ static bytea * dobyteatrim (bytea * string , bytea * set ,
28+ bool doltrim , bool dortrim );
2729
2830
2931/********************************************************************
@@ -521,27 +523,12 @@ dotrim(const char *string, int stringlen,
521523 return cstring_to_text_with_len (string , stringlen );
522524}
523525
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 )
542531{
543- bytea * string = PG_GETARG_BYTEA_PP (0 );
544- bytea * set = PG_GETARG_BYTEA_PP (1 );
545532 bytea * ret ;
546533 char * ptr ,
547534 * end ,
@@ -556,47 +543,134 @@ byteatrim(PG_FUNCTION_ARGS)
556543 setlen = VARSIZE_ANY_EXHDR (set );
557544
558545 if (stringlen <= 0 || setlen <= 0 )
559- PG_RETURN_BYTEA_P ( string ) ;
546+ return string ;
560547
561548 m = stringlen ;
562549 ptr = VARDATA_ANY (string );
563550 end = ptr + stringlen - 1 ;
564551 ptr2start = VARDATA_ANY (set );
565552 end2 = ptr2start + setlen - 1 ;
566553
567- while ( m > 0 )
554+ if ( doltrim )
568555 {
569- ptr2 = ptr2start ;
570- while (ptr2 <= end2 )
556+ while (m > 0 )
571557 {
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 )
573566 break ;
574- ++ ptr2 ;
567+ ptr ++ ;
568+ m -- ;
575569 }
576- if (ptr2 > end2 )
577- break ;
578- ptr ++ ;
579- m -- ;
580570 }
581571
582- while ( m > 0 )
572+ if ( dortrim )
583573 {
584- ptr2 = ptr2start ;
585- while (ptr2 <= end2 )
574+ while (m > 0 )
586575 {
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 )
588584 break ;
589- ++ ptr2 ;
585+ end -- ;
586+ m -- ;
590587 }
591- if (ptr2 > end2 )
592- break ;
593- end -- ;
594- m -- ;
595588 }
596589
597590 ret = (bytea * ) palloc (VARHDRSZ + m );
598591 SET_VARSIZE (ret , VARHDRSZ + m );
599592 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);
600674
601675 PG_RETURN_BYTEA_P (ret );
602676}
0 commit comments