From 451138141f9dc4417436b4d57d303398aa81182f Mon Sep 17 00:00:00 2001 From: Jack Stouffer Date: Sun, 7 May 2017 19:34:45 -0400 Subject: [PATCH] Deprecate obsolete pattern matching functions in std.string --- std/string.d | 52 ++++++++++++++++++++++++++++++++++--------- std/xml.d | 63 ++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 86 insertions(+), 29 deletions(-) diff --git a/std/string.d b/std/string.d index 5e4103366b8..fa59969fed7 100644 --- a/std/string.d +++ b/std/string.d @@ -5230,8 +5230,13 @@ body assert(buffer.data == "h5 rd"); } - +//@@@DEPRECATED_2018-05@@@ /*********************************************** + * $(RED This function is deprecated and will be removed May 2018.) + * Please use the functions in $(MREF std, regex) and $(MREF std, algorithm) + * instead. If you still need this function, it will be available in + * $(LINK2 https://github.com/dlang/undeaD, undeaD). + * * See if character c is in the pattern. * Patterns: * @@ -5248,7 +5253,7 @@ body * Note: In the future, the pattern syntax may be improved * to be more like regular expression character classes. */ - +deprecated("This function is obsolete and will be removed May 2018. See the docs for more details") bool inPattern(S)(dchar c, in S pattern) @safe pure @nogc if (isSomeString!S) { @@ -5314,11 +5319,16 @@ if (isSomeString!S) }); } - +//@@@DEPRECATED_2018-05@@@ /*********************************************** + * $(RED This function is deprecated and will be removed May 2018.) + * Please use the functions in $(MREF std, regex) and $(MREF std, algorithm) + * instead. If you still need this function, it will be available in + * $(LINK2 https://github.com/dlang/undeaD, undeaD). + * * See if character c is in the intersection of the patterns. */ - +deprecated("This function is obsolete and will be removed May 2018. See the docs for more details") bool inPattern(S)(dchar c, S[] patterns) @safe pure @nogc if (isSomeString!S) { @@ -5332,11 +5342,16 @@ if (isSomeString!S) return true; } - +//@@@DEPRECATED_2018-05@@@ /******************************************** + * $(RED This function is deprecated and will be removed May 2018.) + * Please use the functions in $(MREF std, regex) and $(MREF std, algorithm) + * instead. If you still need this function, it will be available in + * $(LINK2 https://github.com/dlang/undeaD, undeaD). + * * Count characters in s that match pattern. */ - +deprecated("This function is obsolete and will be removed May 2018. See the docs for more details") size_t countchars(S, S1)(S s, in S1 pattern) @safe pure @nogc if (isSomeString!S && isSomeString!S1) { @@ -5362,11 +5377,16 @@ if (isSomeString!S && isSomeString!S1) }); } - +//@@@DEPRECATED_2018-05@@@ /******************************************** + * $(RED This function is deprecated and will be removed May 2018.) + * Please use the functions in $(MREF std, regex) and $(MREF std, algorithm) + * instead. If you still need this function, it will be available in + * $(LINK2 https://github.com/dlang/undeaD, undeaD). + * * Return string that is s with all characters removed that match pattern. */ - +deprecated("This function is obsolete and will be removed May 2018. See the docs for more details") S removechars(S)(S s, in S pattern) @safe pure if (isSomeString!S) { @@ -5418,13 +5438,18 @@ if (isSomeString!S) assert(removechars("abc", "x") == "abc"); } - +//@@@DEPRECATED_2018-05@@@ /*************************************************** + * $(RED This function is deprecated and will be removed May 2018.) + * Please use the functions in $(MREF std, regex) and $(MREF std, algorithm) + * instead. If you still need this function, it will be available in + * $(LINK2 https://github.com/dlang/undeaD, undeaD). + * * Return string where sequences of a character in s[] from pattern[] * are replaced with a single instance of that character. * If pattern is null, it defaults to all characters. */ - +deprecated("This function is obsolete and will be removed May 2018. See the docs for more details") S squeeze(S)(S s, in S pattern = null) { import std.utf : encode, stride; @@ -5490,7 +5515,13 @@ S squeeze(S)(S s, in S pattern = null) }); } +//@@@DEPRECATED_2018-05@@@ /*************************************************************** + $(RED This function is deprecated and will be removed May 2018.) + Please use the functions in $(MREF std, regex) and $(MREF std, algorithm) + instead. If you still need this function, it will be available in + $(LINK2 https://github.com/dlang/undeaD, undeaD). + Finds the position $(D_PARAM pos) of the first character in $(D_PARAM s) that does not match $(D_PARAM pattern) (in the terminology used by $(REF inPattern, std,string)). Updates $(D_PARAM s = @@ -5501,6 +5532,7 @@ The $(D_PARAM munch) function is mostly convenient for skipping certain category of characters (e.g. whitespace) when parsing strings. (In such cases, the return value is not used.) */ +deprecated("This function is obsolete and will be removed May 2018. See the docs for more details") S1 munch(S1, S2)(ref S1 s, S2 pattern) @safe pure @nogc { size_t j = s.length; diff --git a/std/xml.d b/std/xml.d index c3374cb8258..d2a28d87c8c 100644 --- a/std/xml.d +++ b/std/xml.d @@ -1056,27 +1056,42 @@ class Tag */ private this(ref string s, bool dummy) @safe pure { - import std.ascii : whitespace; - import std.string : munch; + import std.algorithm.searching : countUntil; + import std.ascii : isWhite; + import std.utf : byCodeUnit; tagString = s; try { reqc(s,'<'); if (optc(s,'/')) type = TagType.END; - name = munch(s,"^/>"~whitespace); - munch(s,whitespace); + ptrdiff_t i = s.byCodeUnit.countUntil(">", "/>", " ", "\t", "\v", "\r", "\n", "\f"); + name = s[0 .. i]; + s = s[i .. $]; + + i = s.byCodeUnit.countUntil!(a => !isWhite(a)); + s = s[i .. $]; + while (s.length > 0 && s[0] != '>' && s[0] != '/') { - string key = munch(s,"^="~whitespace); - munch(s,whitespace); + i = s.byCodeUnit.countUntil("=", " ", "\t", "\v", "\r", "\n", "\f"); + string key = s[0 .. i]; + s = s[i .. $]; + + i = s.byCodeUnit.countUntil!(a => !isWhite(a)); + s = s[i .. $]; reqc(s,'='); - munch(s,whitespace); + i = s.byCodeUnit.countUntil!(a => !isWhite(a)); + s = s[i .. $]; + immutable char quote = requireOneOf(s,"'\""); - char[2] notQuote = ['^', quote]; - string val = decode(munch(s,notQuote[]), DecodeMode.LOOSE); + i = s.byCodeUnit.countUntil(quote); + string val = decode(s[0 .. i], DecodeMode.LOOSE); + s = s[i .. $]; reqc(s,quote); - munch(s,whitespace); + + i = s.byCodeUnit.countUntil!(a => !isWhite(a)); + s = s[i .. $]; attr[key] = val; } if (optc(s,'/')) @@ -2194,10 +2209,16 @@ private void checkSpace(ref string s) @safe pure // rule 3 { - import std.string : munch; + import std.algorithm.searching : countUntil; + import std.ascii : isWhite; + import std.utf : byCodeUnit; mixin Check!("Whitespace"); - munch(s,"\u0020\u0009\u000A\u000D"); + ptrdiff_t i = s.byCodeUnit.countUntil!(a => !isWhite(a)); + if (i == -1 && s.length > 0 && isWhite(s[0])) + s = s[$ .. $]; + else if (i > -1) + s = s[i .. $]; if (s is old) fail(); } @@ -2222,7 +2243,8 @@ private void checkAttValue(ref string s) @safe pure // rule 10 { - import std.string : munch; + import std.algorithm.searching : countUntil; + import std.utf : byCodeUnit; mixin Check!("AttValue"); @@ -2233,7 +2255,7 @@ private s = s[1..$]; for (;;) { - munch(s,"^<&"~c); + s = s[s.byCodeUnit.countUntil(c) .. $]; if (s.length == 0) fail("unterminated attribute value"); if (s[0] == '<') fail("< found in attribute value"); if (s[0] == c) break; @@ -2356,11 +2378,12 @@ private void checkVersionNum(ref string s) @safe pure // rule 26 { - import std.string : munch; + import std.algorithm.searching : countUntil; + import std.utf : byCodeUnit; mixin Check!("VersionNum"); - munch(s,"a-zA-Z0-9_.:-"); + s = s[s.byCodeUnit.countUntil('\"') .. $]; if (s is old) fail(); } @@ -2583,13 +2606,15 @@ private void checkEncName(ref string s) @safe pure // rule 81 { - import std.string : munch; + import std.algorithm.searching : countUntil; + import std.ascii : isAlpha; + import std.utf : byCodeUnit; mixin Check!("EncName"); - munch(s,"a-zA-Z"); + s = s[s.byCodeUnit.countUntil!(a => !isAlpha(a)) .. $]; if (s is old) fail(); - munch(s,"a-zA-Z0-9_.-"); + s = s[s.byCodeUnit.countUntil('\"', '\'') .. $]; } void checkEncodingDecl(ref string s) @safe pure // rule 80