From 59954acb491858cf7222b13de23440cd960154d6 Mon Sep 17 00:00:00 2001 From: Cameron Ross Date: Wed, 16 Jan 2019 02:53:48 -0330 Subject: [PATCH] change canFind() to among() where possible (#221) change canFind() to among() where possible merged-on-behalf-of: BBasile --- source/dyaml/constructor.d | 6 ++--- source/dyaml/emitter.d | 18 +++++++-------- source/dyaml/escapes.d | 8 +++---- source/dyaml/parser.d | 4 ++-- source/dyaml/scanner.d | 46 ++++++++++++++++++------------------- source/dyaml/test/emitter.d | 6 ++--- 6 files changed, 42 insertions(+), 46 deletions(-) diff --git a/source/dyaml/constructor.d b/source/dyaml/constructor.d index 13dbac0..f521bf6 100644 --- a/source/dyaml/constructor.d +++ b/source/dyaml/constructor.d @@ -190,11 +190,9 @@ private: // Construct a boolean _node. bool constructBool(const string str) @safe { - static yes = ["yes", "true", "on"]; - static no = ["no", "false", "off"]; string value = str.toLower(); - if(yes.canFind(value)){return true;} - if(no.canFind(value)) {return false;} + if(value.among!("yes", "true", "on")){return true;} + if(value.among!("no", "false", "off")){return false;} throw new Exception("Unable to parse boolean value: " ~ value); } diff --git a/source/dyaml/emitter.d b/source/dyaml/emitter.d index cd74507..8ffd53a 100644 --- a/source/dyaml/emitter.d +++ b/source/dyaml/emitter.d @@ -275,10 +275,8 @@ struct Emitter(Range, CharType) if (isOutputRange!(Range, CharType)) foreach(const event; events_.range) { - static starts = [EventID.documentStart, EventID.sequenceStart, EventID.mappingStart]; - static ends = [EventID.documentEnd, EventID.sequenceEnd, EventID.mappingEnd]; - if(starts.canFind(event.id)) {++level;} - else if(ends.canFind(event.id)){--level;} + if(event.id.among!(EventID.documentStart, EventID.sequenceStart, EventID.mappingStart)) {++level;} + else if(event.id.among!(EventID.documentEnd, EventID.sequenceEnd, EventID.mappingEnd)) {--level;} else if(event.id == EventID.streamStart){level = -1;} if(level < 0) @@ -893,7 +891,7 @@ struct Emitter(Range, CharType) if (isOutputRange!(Range, CharType)) if(handle.length > 1) foreach(const dchar c; handle[1 .. $ - 1]) { - enforce(isAlphaNum(c) || "-_"d.canFind(c), + enforce(isAlphaNum(c) || c.among!('-', '_'), new EmitterException("Invalid character: " ~ to!string(c) ~ " in tag handle " ~ handle)); } @@ -913,7 +911,7 @@ struct Emitter(Range, CharType) if (isOutputRange!(Range, CharType)) foreach(const size_t i, const dchar c; prefix) { const size_t idx = i + offset; - if(isAlphaNum(c) || "-;/?:@&=+$,_.!~*\'()[]%"d.canFind(c)) + if(isAlphaNum(c) || c.among!('-', ';', '/', '?', ':', '@', '&', '=', '+', '$', ',', '_', '.', '!', '~', '*', '\\', '\'', '(', ')', '[', ']', '%')) { end = idx + 1; continue; @@ -958,7 +956,7 @@ struct Emitter(Range, CharType) if (isOutputRange!(Range, CharType)) size_t start, end; foreach(const dchar c; suffix) { - if(isAlphaNum(c) || "-;/?:@&=+$,_.~*\'()[]"d.canFind(c) || + if(isAlphaNum(c) || c.among!('-', ';', '/', '?', ':', '@', '&', '=', '+', '$', ',', '_', '.', '~', '*', '\\', '\'', '(', ')', '[', ']') || (c == '!' && handle != "!")) { ++end; @@ -984,7 +982,7 @@ struct Emitter(Range, CharType) if (isOutputRange!(Range, CharType)) const str = anchor; foreach(const dchar c; str) { - enforce(isAlphaNum(c) || "-_"d.canFind(c), + enforce(isAlphaNum(c) || c.among!('-', '_'), new EmitterException("Invalid character: " ~ to!string(c) ~ " in anchor: " ~ str)); } return str; @@ -1027,7 +1025,7 @@ struct Emitter(Range, CharType) if (isOutputRange!(Range, CharType)) //Last character or followed by a whitespace. bool followedByWhitespace = scalar.length == 1 || - " \t\0\n\r\u0085\u2028\u2029"d.canFind(scalar[1]); + scalar[1].among!(' ', '\t', '\0', '\n', '\r', '\u0085', '\u2028', '\u2029'); //The previous character is a space/break (false by default). bool previousSpace, previousBreak; @@ -1412,7 +1410,7 @@ struct ScalarWriter(Range, CharType) { const dchar c = nextChar(); //handle special characters - if(c == dcharNone || "\"\\\u0085\u2028\u2029\uFEFF"d.canFind(c) || + if(c == dcharNone || c.among!('\"', '\\', '\u0085', '\u2028', '\u2029', '\uFEFF') || !((c >= '\x20' && c <= '\x7E') || ((c >= '\xA0' && c <= '\uD7FF') || (c >= '\uE000' && c <= '\uFFFD')))) { diff --git a/source/dyaml/escapes.d b/source/dyaml/escapes.d index a5fdbc2..32080a2 100644 --- a/source/dyaml/escapes.d +++ b/source/dyaml/escapes.d @@ -9,12 +9,12 @@ module dyaml.escapes; package: -/// All YAML escapes. -immutable dchar[] escapes = ['0', 'a', 'b', 't', '\t', 'n', 'v', 'f', 'r', 'e', ' ', - '\"', '\\', 'N', '_', 'L', 'P']; +import std.meta : AliasSeq; +alias escapes = AliasSeq!('0', 'a', 'b', 't', '\t', 'n', 'v', 'f', 'r', 'e', ' ', + '\"', '\\', 'N', '_', 'L', 'P'); /// YAML hex codes specifying the length of the hex number. -immutable dchar[] escapeHexCodeList = ['x', 'u', 'U']; +alias escapeHexCodeList = AliasSeq!('x', 'u', 'U'); /// Convert a YAML escape to a dchar. dchar fromEscape(dchar escape) @safe pure nothrow @nogc diff --git a/source/dyaml/parser.d b/source/dyaml/parser.d index df6f4a7..4840296 100644 --- a/source/dyaml/parser.d +++ b/source/dyaml/parser.d @@ -528,7 +528,7 @@ final class Parser scope(exit) { inEscape = false; } // 'Normal' escape sequence. - if(dyaml.escapes.escapes.canFind(c)) + if(c.among!(escapes)) { if(notInPlace is null) { @@ -550,7 +550,7 @@ final class Parser } // Unicode char written in hexadecimal in an escape sequence. - if(dyaml.escapes.escapeHexCodeList.canFind(c)) + if(c.among!(escapeHexCodeList)) { // Scanner has already checked that the hex string is valid. diff --git a/source/dyaml/scanner.d b/source/dyaml/scanner.d index da13bc8..0f32a11 100644 --- a/source/dyaml/scanner.d +++ b/source/dyaml/scanner.d @@ -835,7 +835,7 @@ struct Scanner { size_t length; dchar c = reader_.peek(); - while(c.isAlphaNum || "-_"d.canFind(c)) { c = reader_.peek(++length); } + while(c.isAlphaNum || c.among!('-', '_')) { c = reader_.peek(++length); } if(length == 0) { @@ -958,7 +958,7 @@ struct Scanner scanAlphaNumericToSlice!"a directive"(startMark); if(error_) { return; } - if(" \0\n\r\u0085\u2028\u2029"d.canFind(reader_.peek())) { return; } + if(reader_.peek().among!(' ', '\0', '\n', '\r', '\u0085', '\u2028', '\u2029')) { return; } error("While scanning a directive", startMark, expected("alphanumeric, '-' or '_'", reader_.peek()), reader_.mark); } @@ -989,7 +989,7 @@ struct Scanner scanYAMLDirectiveNumberToSlice(startMark); if(error_) { return; } - if(!" \0\n\r\u0085\u2028\u2029"d.canFind(reader_.peek())) + if(!reader_.peek().among!(' ', '\0', '\n', '\r', '\u0085', '\u2028', '\u2029')) { error("While scanning a directive", startMark, expected("digit or '.'", reader_.peek()), reader_.mark); @@ -1063,7 +1063,7 @@ struct Scanner void scanTagDirectivePrefixToSlice(const Mark startMark) @safe { scanTagURIToSlice!"directive"(startMark); - if(" \0\n\r\u0085\u2028\u2029"d.canFind(reader_.peek())) { return; } + if(reader_.peek().among!(' ', '\0', '\n', '\r', '\u0085', '\u2028', '\u2029')) { return; } error("While scanning a directive prefix", startMark, expected("' '", reader_.peek()), reader_.mark); } @@ -1110,7 +1110,7 @@ struct Scanner if(error_) { return Token.init; } if(!reader_.peek().isWhiteSpace && - !"?:,]}%@"d.canFind(reader_.peekByte())) + !reader_.peekByte().among!('?', ':', ',', ']', '}', '%', '@')) { enum anchorCtx = "While scanning an anchor"; enum aliasCtx = "While scanning an alias"; @@ -1257,7 +1257,7 @@ struct Scanner while(reader_.column == indent && reader_.peekByte() != '\0') { breaksTransaction.commit(); - const bool leadingNonSpace = !" \t"d.canFind(reader_.peekByte()); + const bool leadingNonSpace = !reader_.peekByte().among!(' ', '\t'); // This is where the 'interesting' non-whitespace data gets read. scanToNextBreakToSlice(); lineBreak = scanLineBreak(); @@ -1280,7 +1280,7 @@ struct Scanner // This is the folding according to the specification: if(style == ScalarStyle.folded && lineBreak == '\n' && - leadingNonSpace && !" \t"d.canFind(reader_.peekByte())) + leadingNonSpace && !reader_.peekByte().among!(' ', '\t')) { // No breaks were scanned; no need to insert the space in the // middle of slice. @@ -1373,7 +1373,7 @@ struct Scanner if(gotIncrement) { getChomping(c, chomping); } } - if(" \0\n\r\u0085\u2028\u2029"d.canFind(c)) + if(c.among!(' ', '\0', '\n', '\r', '\u0085', '\u2028', '\u2029')) { return tuple(chomping, increment); } @@ -1392,7 +1392,7 @@ struct Scanner /// chomping = Write the chomping value here, if detected. bool getChomping(ref dchar c, ref Chomping chomping) @safe { - if(!"+-"d.canFind(c)) { return false; } + if(!c.among!('+', '-')) { return false; } chomping = c == '+' ? Chomping.keep : Chomping.strip; reader_.forward(); c = reader_.peek(); @@ -1455,7 +1455,7 @@ struct Scanner uint maxIndent; Mark endMark = reader_.mark; - while(" \n\r\u0085\u2028\u2029"d.canFind(reader_.peek())) + while(reader_.peek().among!(' ', '\n', '\r', '\u0085', '\u2028', '\u2029')) { if(reader_.peekByte() != ' ') { @@ -1481,7 +1481,7 @@ struct Scanner for(;;) { while(reader_.column < indent && reader_.peekByte() == ' ') { reader_.forward(); } - if(!"\n\r\u0085\u2028\u2029"d.canFind(reader_.peek())) { break; } + if(!reader_.peek().among!('\n', '\r', '\u0085', '\u2028', '\u2029')) { break; } reader_.sliceBuilder.write(scanLineBreak()); endMark = reader_.mark; } @@ -1562,7 +1562,7 @@ struct Scanner reader_.sliceBuilder.write('\''); } else if((quotes == ScalarStyle.doubleQuoted && c == '\'') || - (quotes == ScalarStyle.singleQuoted && "\"\\"d.canFind(c))) + (quotes == ScalarStyle.singleQuoted && c.among!('"', '\\'))) { reader_.forward(); reader_.sliceBuilder.write(c); @@ -1571,7 +1571,7 @@ struct Scanner { reader_.forward(); c = reader_.peek(); - if(dyaml.escapes.escapes.canFind(c)) + if(c.among!(escapes)) { reader_.forward(); // Escaping has been moved to Parser as it can't be done in @@ -1580,7 +1580,7 @@ struct Scanner char[2] escapeSequence = ['\\', cast(char)c]; reader_.sliceBuilder.write(escapeSequence); } - else if(dyaml.escapes.escapeHexCodeList.canFind(c)) + else if(c.among!(escapeHexCodeList)) { const hexLength = dyaml.escapes.escapeHexLength(c); reader_.forward(); @@ -1612,7 +1612,7 @@ struct Scanner return; } } - else if("\n\r\u0085\u2028\u2029"d.canFind(c)) + else if(c.among!('\n', '\r', '\u0085', '\u2028', '\u2029')) { scanLineBreak(); scanFlowScalarBreaksToSlice(startMark); @@ -1640,7 +1640,7 @@ struct Scanner { // Increase length as long as we see whitespace. size_t length; - while(" \t"d.canFind(reader_.peekByte(length))) { ++length; } + while(reader_.peekByte(length).among!(' ', '\t')) { ++length; } auto whitespaces = reader_.prefixBytes(length); // Can check the last byte without striding because '\0' is ASCII @@ -1653,7 +1653,7 @@ struct Scanner } // Spaces not followed by a line break. - if(!"\n\r\u0085\u2028\u2029"d.canFind(c)) + if(!c.among!('\n', '\r', '\u0085', '\u2028', '\u2029')) { reader_.forward(length); reader_.sliceBuilder.write(whitespaces); @@ -1698,10 +1698,10 @@ struct Scanner } // Skip any whitespaces. - while(" \t"d.canFind(reader_.peekByte())) { reader_.forward(); } + while(reader_.peekByte().among!(' ', '\t')) { reader_.forward(); } // Encountered a non-whitespace non-linebreak character, so we're done. - if(!"\n\r\u0085\u2028\u2029"d.canFind(reader_.peek())) { break; } + if(!reader_.peek().among!(' ', '\n', '\r', '\u0085', '\u2028', '\u2029')) { break; } const lineBreak = scanLineBreak(); anyBreaks = true; @@ -1756,7 +1756,7 @@ struct Scanner for(;;) { c = reader_.peek(length); - if(c.isWhiteSpace || ",:?[]{}"d.canFind(c)) + if(c.isWhiteSpace || c.among!(',', ':', '?', '[', ']', '{', '}')) { break; } @@ -1767,7 +1767,7 @@ struct Scanner // It's not clear what we should do with ':' in the flow context. if(flowLevel_ > 0 && c == ':' && !reader_.peek(length + 1).isWhiteSpace && - !",[]{}"d.canFind(reader_.peek(length + 1))) + !reader_.peek(length + 1).among!(',', '[', ']', '{', '}')) { // This is an error; throw the slice away. spacesTransaction.commit(); @@ -1837,7 +1837,7 @@ struct Scanner { const prefix = reader_.prefix(3); return ("---" == prefix || "..." == prefix) - && " \t\0\n\r\u0085\u2028\u2029"d.canFind(reader_.peek(3)); + && reader_.peek(3).among!(' ', '\t', '\0', '\n', '\r', '\u0085', '\u2028', '\u2029'); } if(end(reader_)) { return; } @@ -1885,7 +1885,7 @@ struct Scanner c = reader_.peek(length); if(c != ' ') { - while(c.isAlphaNum || "-_"d.canFind(c)) + while(c.isAlphaNum || c.among!('-', '_')) { ++length; c = reader_.peek(length); diff --git a/source/dyaml/test/emitter.d b/source/dyaml/test/emitter.d index e55bed0..30082a4 100644 --- a/source/dyaml/test/emitter.d +++ b/source/dyaml/test/emitter.d @@ -35,16 +35,16 @@ bool compareEvents(T, U)(T events1, U events2) //Different event types. if(e1.id != e2.id){return false;} //Different anchor (if applicable). - if([EventID.sequenceStart, + if(e1.id.among!(EventID.sequenceStart, EventID.mappingStart, EventID.alias_, - EventID.scalar].canFind(e1.id) + EventID.scalar) && e1.anchor != e2.anchor) { return false; } //Different collection tag (if applicable). - if([EventID.sequenceStart, EventID.mappingStart].canFind(e1.id) && e1.tag != e2.tag) + if(e1.id.among!(EventID.sequenceStart, EventID.mappingStart) && e1.tag != e2.tag) { return false; }