@@ -31,20 +31,62 @@ sub count-substrs(Str:D $ip, Str:D $substr --> UInt) is export(:count-substrs) {
3131# | Purpose : Strip comments from an input text line, save comment if
3232# | requested, normalize returned text if requested
3333# | Params : String of text, comment char ('#' is default),
34+ # | option to return the comment, option to normalize
35+ # | the returned strings, option to use the last comment char
36+ # | instead of the first
3437# | Returns : String of text with any comment stripped off. Note that the
3538# | designated char will trigger the strip even though it is
3639# | escaped or included in quotes.
3740# | Also returns the comment if requested.
3841# | All returned text is normalized if requested.
39- sub strip-comment ($ line is copy , # = string of text with possible comment
40- $ comment-char = ' #' , # = desired comment char indicator
41- : $ save-comment , # = if true, return the comment
42- : $ normalize , # = if true, normalize returned strings
43- ) is export (: strip-comment) {
42+ multi strip-comment ($ line is copy , # = string of text with possible comment
43+ : $ mark = ' #' , # = desired comment char indicator
44+ : $ save-comment , # = if true, return the comment
45+ : $ normalize , # = if true, normalize returned strings
46+ : $ last , # = if true, use the last instead of first comment char
47+ ) is export (: strip-comment) {
48+ # failure condition:
49+ if $ line eq $ mark {
50+ die " FATAL: The input line is the same as the comment char: '$ line '" ;
51+ }
52+ my $ comment = ' ' ;
53+ my $ clen = $ mark . chars ;
54+ my $ idx = $ last ?? rindex $ line , $ mark
55+ !! index $ line , $ mark ;
56+ if $ idx . defined {
57+ $ comment = substr $ line , $ idx + $ clen ; # = don't want the comment char
58+ $ line = substr $ line , 0 , $ idx ;
59+ }
60+ if $ normalize {
61+ $ line = normalize-string $ line ;
62+ $ comment = normalize-string $ comment ;
63+ }
64+ if $ save-comment {
65+ return $ line , $ comment ;
66+ }
67+ $ line ;
68+ }
69+
70+ # | NOTE THE FOLLOWING SIGNATURE IS DEPRECATED
71+ multi strip-comment ($ line is copy , # = string of text with possible comment
72+ $ comment-char = ' #' , # = desired comment char indicator
73+ : $ save-comment , # = if true, return the comment
74+ : $ normalize , # = if true, normalize returned strings
75+ : $ last , # = if true, use the last instead of first comment char
76+ ) is export (: strip-comment) {
77+ # failure condition:
78+ if $ line eq $ comment-char {
79+ note " FATAL: The input line is the same as the comment char: '$ line '" ;
80+ note " This signature is deprecated." ;
81+ note " Use the ':mark<$ line >' named param for the comment char." ;
82+ die " DEPRECATED SIGNATURE--USE THE MARK NOTATION IN THE FUTURE" ;
83+ }
4484 my $ comment = ' ' ;
45- my $ idx = index $ line , $ comment-char ;
85+ my $ clen = $ comment-char . chars ;
86+ my $ idx = $ last ?? rindex $ line , $ comment-char
87+ !! index $ line , $ comment-char ;
4688 if $ idx . defined {
47- $ comment = substr $ line , $ idx + 1 ; # = don't want the comment char
89+ $ comment = substr $ line , $ idx + $ clen ; # = don't want the comment char
4890 $ line = substr $ line , 0 , $ idx ;
4991 }
5092 if $ normalize {
0 commit comments