Skip to content

Commit 6f4e561

Browse files
committed
Add START_WITH and END_WITH filter.
Add corresponding regression tests and documentation. Motivated by eng/toolchain/aws#39
1 parent 5e15c8f commit 6f4e561

File tree

7 files changed

+151
-8
lines changed

7 files changed

+151
-8
lines changed

docs/tags.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,12 @@ parser.
345345
character if needed. Returns the input string as-is if the parameter
346346
is not found.
347347

348+
**END_WITH(VALUE)**
349+
.. index:: Filter, END_WITH
350+
351+
Returns **True** if variable ends with VALUE given as parameter
352+
and **False** otherwise.
353+
348354
**EXIST**
349355
.. index:: Filter, EXIST
350356

@@ -585,6 +591,12 @@ parser.
585591
that 0 matches the last character of the string, -1 matches the character
586592
just before,...
587593

594+
**START_WITH(VALUE)**
595+
.. index:: Filter, START_WITH
596+
597+
Returns **True** if variable starts with VALUE given as parameter
598+
and **False** otherwise.
599+
588600
**STRIP**
589601
.. index:: Filter, STRIP
590602

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
Test START_WITH() routine.
3+
4+
TRUE = TRUE
5+
TRUE = TRUE
6+
TRUE = TRUE
7+
TRUE = TRUE
8+
FALSE = FALSE
9+
FALSE = FALSE
10+
FALSE = FALSE
11+
FALSE = FALSE
12+
FALSE = FALSE
13+
14+
Test END_WITH() routine.
15+
16+
TRUE = TRUE
17+
TRUE = TRUE
18+
TRUE = TRUE
19+
TRUE = TRUE
20+
FALSE = FALSE
21+
FALSE = FALSE
22+
FALSE = FALSE
23+
FALSE = FALSE
24+
FALSE = FALSE
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from test_support import *
2+
3+
run('testme', ["testme144.tmplt"])

regtests/tests/0144_start_end_with/test.yaml

Whitespace-only changes.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
Test START_WITH() routine.
3+
4+
TRUE = @_START_WITH(a):VAR1_@
5+
TRUE = @_START_WITH(a ):VAR1_@
6+
TRUE = @_START_WITH(a valu):VAR1_@
7+
TRUE = @_START_WITH(a value):VAR1_@
8+
FALSE = @_START_WITH(w):VAR1_@
9+
FALSE = @_START_WITH(x value):VAR1_@
10+
FALSE = @_START_WITH(a valux):VAR1_@
11+
FALSE = @_START_WITH( value):VAR1_@
12+
FALSE = @_START_WITH(xxxxxxxxxxxxxxxxxxxxxxxxxxx):VAR1_@
13+
14+
Test END_WITH() routine.
15+
16+
TRUE = @_END_WITH(e):VAR1_@
17+
TRUE = @_END_WITH(lue):VAR1_@
18+
TRUE = @_END_WITH( value):VAR1_@
19+
TRUE = @_END_WITH(a value):VAR1_@
20+
FALSE = @_END_WITH(w):VAR1_@
21+
FALSE = @_END_WITH(x value):VAR1_@
22+
FALSE = @_END_WITH(a valux):VAR1_@
23+
FALSE = @_END_WITH(a valu):VAR1_@
24+
FALSE = @_END_WITH(xxxxxxxxxxxxxxxxxxxxxxxxxxxx):VAR1_@

src/templates_parser-filter.adb

Lines changed: 72 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,21 @@ package body Filter is
5252

5353
-- Filter tokens
5454

55-
Multiply_Token : aliased constant String := """*""";
56-
Plus_Token : aliased constant String := """+""";
57-
Minus_Token : aliased constant String := """-""";
58-
Divide_Token : aliased constant String := """/""";
5955
Abs_Token : aliased constant String := "ABS";
60-
Add_Token : aliased constant String := "ADD";
6156
Add_Param_Token : aliased constant String := "ADD_PARAM";
57+
Add_Token : aliased constant String := "ADD";
58+
Alternate_Token : aliased constant String := "ALTERNATE";
6259
BR_2_EOL_Token : aliased constant String := "BR_2_EOL";
6360
BR_2_LF_Token : aliased constant String := "BR_2_LF";
6461
Capitalize_Token : aliased constant String := "CAPITALIZE";
6562
Clean_Text_Token : aliased constant String := "CLEAN_TEXT";
6663
Coma_2_Point_Token : aliased constant String := "COMA_2_POINT";
6764
Contract_Token : aliased constant String := "CONTRACT";
65+
Default_Token : aliased constant String := "DEFAULT";
6866
Del_Param_Token : aliased constant String := "DEL_PARAM";
6967
Div_Token : aliased constant String := "DIV";
68+
Divide_Token : aliased constant String := """/""";
69+
End_With_Token : aliased constant String := "END_WITH";
7070
Exist_Token : aliased constant String := "EXIST";
7171
File_Exists_Token : aliased constant String := "FILE_EXISTS";
7272
Format_Date_Token : aliased constant String := "FORMAT_DATE";
@@ -77,22 +77,26 @@ package body Filter is
7777
Match_Token : aliased constant String := "MATCH";
7878
Max_Token : aliased constant String := "MAX";
7979
Min_Token : aliased constant String := "MIN";
80+
Minus_Token : aliased constant String := """-""";
8081
Modulo_Token : aliased constant String := "MOD";
8182
Mult_Token : aliased constant String := "MULT";
83+
Multiply_Token : aliased constant String := """*""";
8284
Neg_Token : aliased constant String := "NEG";
8385
No_Digit_Token : aliased constant String := "NO_DIGIT";
8486
No_Dynamic_Token : aliased constant String := "NO_DYNAMIC";
8587
No_Letter_Token : aliased constant String := "NO_LETTER";
8688
No_Space_Token : aliased constant String := "NO_SPACE";
8789
Oui_Non_Token : aliased constant String := "OUI_NON";
90+
Plus_Token : aliased constant String := """+""";
8891
Point_2_Coma_Token : aliased constant String := "POINT_2_COMA";
8992
Repeat_Token : aliased constant String := "REPEAT";
90-
Replace_Token : aliased constant String := "REPLACE";
9193
Replace_All_Token : aliased constant String := "REPLACE_ALL";
9294
Replace_Param_Token : aliased constant String := "REPLACE_PARAM";
95+
Replace_Token : aliased constant String := "REPLACE";
9396
Reverse_Token : aliased constant String := "REVERSE";
9497
Size_Token : aliased constant String := "SIZE";
9598
Slice_Token : aliased constant String := "SLICE";
99+
Start_With_Token : aliased constant String := "START_WITH";
96100
Strip_Token : aliased constant String := "STRIP";
97101
Sub_Token : aliased constant String := "SUB";
98102
Trim_Token : aliased constant String := "TRIM";
@@ -103,8 +107,6 @@ package body Filter is
103107
Web_NBSP_Token : aliased constant String := "WEB_NBSP";
104108
Wrap_Token : aliased constant String := "WRAP";
105109
Yes_No_Token : aliased constant String := "YES_NO";
106-
Default_Token : aliased constant String := "DEFAULT";
107-
Alternate_Token : aliased constant String := "ALTERNATE";
108110

109111
-- Filters Table
110112

@@ -265,6 +267,12 @@ package body Filter is
265267
Wrap =>
266268
(Wrap_Token'Access, Wrap'Access),
267269

270+
Start_With =>
271+
(Start_With_Token'Access, Start_With'Access),
272+
273+
End_With =>
274+
(End_With_Token'Access, End_With'Access),
275+
268276
Yes_No =>
269277
(Yes_No_Token'Access, Yes_No'Access)
270278
];
@@ -708,6 +716,34 @@ package body Filter is
708716
end;
709717
end Divide;
710718

719+
--------------
720+
-- End_With --
721+
--------------
722+
723+
function End_With
724+
(S : String;
725+
C : not null access Filter_Context;
726+
P : Parameter_Data := No_Parameter) return String
727+
is
728+
pragma Unreferenced (C);
729+
begin
730+
if P = No_Parameter then
731+
raise Template_Error with "missing parameter for END_WITH filter";
732+
end if;
733+
734+
declare
735+
Sub : constant String := To_String (P.S);
736+
begin
737+
if S'Length >= Sub'Length
738+
and then S (S'Last - Sub'Length + 1 .. S'Last) = Sub
739+
then
740+
return "TRUE";
741+
else
742+
return "FALSE";
743+
end if;
744+
end;
745+
end End_With;
746+
711747
-----------
712748
-- Exist --
713749
-----------
@@ -1785,6 +1821,34 @@ package body Filter is
17851821
end if;
17861822
end Slice;
17871823

1824+
----------------
1825+
-- Start_With --
1826+
----------------
1827+
1828+
function Start_With
1829+
(S : String;
1830+
C : not null access Filter_Context;
1831+
P : Parameter_Data := No_Parameter) return String
1832+
is
1833+
pragma Unreferenced (C);
1834+
begin
1835+
if P = No_Parameter then
1836+
raise Template_Error with "missing parameter for START_WITH filter";
1837+
end if;
1838+
1839+
declare
1840+
Sub : constant String := To_String (P.S);
1841+
begin
1842+
if S'Length >= Sub'Length
1843+
and then S (S'First .. S'First + Sub'Length - 1) = Sub
1844+
then
1845+
return "TRUE";
1846+
else
1847+
return "FALSE";
1848+
end if;
1849+
end;
1850+
end Start_With;
1851+
17881852
-----------
17891853
-- Strip --
17901854
-----------

src/templates_parser.adb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ package body Templates_Parser is
161161
Div,
162162
-- Divide the given parameter to the string
163163

164+
End_With,
165+
-- Returns TRUE if string ends with parameter
166+
164167
Exist,
165168
-- Returns "TRUE" if var is not empty and "FALSE" otherwise
166169

@@ -254,6 +257,9 @@ package body Templates_Parser is
254257
Slice,
255258
-- Returns a slice of the string
256259

260+
Start_With,
261+
-- Returns TRUE if string starts with parameter
262+
257263
Strip,
258264
-- Trim leading and trailing spaces and characters HT, LF, CR, NUL
259265

@@ -445,6 +451,11 @@ package body Templates_Parser is
445451
C : not null access Filter_Context;
446452
P : Parameter_Data := No_Parameter) return String;
447453

454+
function End_With
455+
(S : String;
456+
C : not null access Filter_Context;
457+
P : Parameter_Data := No_Parameter) return String;
458+
448459
function Exist
449460
(S : String;
450461
C : not null access Filter_Context;
@@ -565,6 +576,11 @@ package body Templates_Parser is
565576
C : not null access Filter_Context;
566577
P : Parameter_Data := No_Parameter) return String;
567578

579+
function Start_With
580+
(S : String;
581+
C : not null access Filter_Context;
582+
P : Parameter_Data := No_Parameter) return String;
583+
568584
function Strip
569585
(S : String;
570586
C : not null access Filter_Context;

0 commit comments

Comments
 (0)