change canFind() to among() where possible (#221)

change canFind() to among() where possible
merged-on-behalf-of: BBasile <BBasile@users.noreply.github.com>
This commit is contained in:
Cameron Ross 2019-01-16 02:53:48 -03:30 committed by The Dlang Bot
parent 71917d501c
commit 59954acb49
6 changed files with 42 additions and 46 deletions

View file

@ -190,11 +190,9 @@ private:
// Construct a boolean _node. // Construct a boolean _node.
bool constructBool(const string str) @safe bool constructBool(const string str) @safe
{ {
static yes = ["yes", "true", "on"];
static no = ["no", "false", "off"];
string value = str.toLower(); string value = str.toLower();
if(yes.canFind(value)){return true;} if(value.among!("yes", "true", "on")){return true;}
if(no.canFind(value)) {return false;} if(value.among!("no", "false", "off")){return false;}
throw new Exception("Unable to parse boolean value: " ~ value); throw new Exception("Unable to parse boolean value: " ~ value);
} }

View file

@ -275,10 +275,8 @@ struct Emitter(Range, CharType) if (isOutputRange!(Range, CharType))
foreach(const event; events_.range) foreach(const event; events_.range)
{ {
static starts = [EventID.documentStart, EventID.sequenceStart, EventID.mappingStart]; if(event.id.among!(EventID.documentStart, EventID.sequenceStart, EventID.mappingStart)) {++level;}
static ends = [EventID.documentEnd, EventID.sequenceEnd, EventID.mappingEnd]; else if(event.id.among!(EventID.documentEnd, EventID.sequenceEnd, EventID.mappingEnd)) {--level;}
if(starts.canFind(event.id)) {++level;}
else if(ends.canFind(event.id)){--level;}
else if(event.id == EventID.streamStart){level = -1;} else if(event.id == EventID.streamStart){level = -1;}
if(level < 0) 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]) 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) ~ new EmitterException("Invalid character: " ~ to!string(c) ~
" in tag handle " ~ handle)); " 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) foreach(const size_t i, const dchar c; prefix)
{ {
const size_t idx = i + offset; const size_t idx = i + offset;
if(isAlphaNum(c) || "-;/?:@&=+$,_.!~*\'()[]%"d.canFind(c)) if(isAlphaNum(c) || c.among!('-', ';', '/', '?', ':', '@', '&', '=', '+', '$', ',', '_', '.', '!', '~', '*', '\\', '\'', '(', ')', '[', ']', '%'))
{ {
end = idx + 1; end = idx + 1;
continue; continue;
@ -958,7 +956,7 @@ struct Emitter(Range, CharType) if (isOutputRange!(Range, CharType))
size_t start, end; size_t start, end;
foreach(const dchar c; suffix) foreach(const dchar c; suffix)
{ {
if(isAlphaNum(c) || "-;/?:@&=+$,_.~*\'()[]"d.canFind(c) || if(isAlphaNum(c) || c.among!('-', ';', '/', '?', ':', '@', '&', '=', '+', '$', ',', '_', '.', '~', '*', '\\', '\'', '(', ')', '[', ']') ||
(c == '!' && handle != "!")) (c == '!' && handle != "!"))
{ {
++end; ++end;
@ -984,7 +982,7 @@ struct Emitter(Range, CharType) if (isOutputRange!(Range, CharType))
const str = anchor; const str = anchor;
foreach(const dchar c; str) 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)); new EmitterException("Invalid character: " ~ to!string(c) ~ " in anchor: " ~ str));
} }
return str; return str;
@ -1027,7 +1025,7 @@ struct Emitter(Range, CharType) if (isOutputRange!(Range, CharType))
//Last character or followed by a whitespace. //Last character or followed by a whitespace.
bool followedByWhitespace = scalar.length == 1 || 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). //The previous character is a space/break (false by default).
bool previousSpace, previousBreak; bool previousSpace, previousBreak;
@ -1412,7 +1410,7 @@ struct ScalarWriter(Range, CharType)
{ {
const dchar c = nextChar(); const dchar c = nextChar();
//handle special characters //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 >= '\x20' && c <= '\x7E') ||
((c >= '\xA0' && c <= '\uD7FF') || (c >= '\uE000' && c <= '\uFFFD')))) ((c >= '\xA0' && c <= '\uD7FF') || (c >= '\uE000' && c <= '\uFFFD'))))
{ {

View file

@ -9,12 +9,12 @@ module dyaml.escapes;
package: package:
/// All YAML escapes. import std.meta : AliasSeq;
immutable dchar[] escapes = ['0', 'a', 'b', 't', '\t', 'n', 'v', 'f', 'r', 'e', ' ', alias escapes = AliasSeq!('0', 'a', 'b', 't', '\t', 'n', 'v', 'f', 'r', 'e', ' ',
'\"', '\\', 'N', '_', 'L', 'P']; '\"', '\\', 'N', '_', 'L', 'P');
/// YAML hex codes specifying the length of the hex number. /// 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. /// Convert a YAML escape to a dchar.
dchar fromEscape(dchar escape) @safe pure nothrow @nogc dchar fromEscape(dchar escape) @safe pure nothrow @nogc

View file

@ -528,7 +528,7 @@ final class Parser
scope(exit) { inEscape = false; } scope(exit) { inEscape = false; }
// 'Normal' escape sequence. // 'Normal' escape sequence.
if(dyaml.escapes.escapes.canFind(c)) if(c.among!(escapes))
{ {
if(notInPlace is null) if(notInPlace is null)
{ {
@ -550,7 +550,7 @@ final class Parser
} }
// Unicode char written in hexadecimal in an escape sequence. // 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. // Scanner has already checked that the hex string is valid.

View file

@ -835,7 +835,7 @@ struct Scanner
{ {
size_t length; size_t length;
dchar c = reader_.peek(); 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) if(length == 0)
{ {
@ -958,7 +958,7 @@ struct Scanner
scanAlphaNumericToSlice!"a directive"(startMark); scanAlphaNumericToSlice!"a directive"(startMark);
if(error_) { return; } 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, error("While scanning a directive", startMark,
expected("alphanumeric, '-' or '_'", reader_.peek()), reader_.mark); expected("alphanumeric, '-' or '_'", reader_.peek()), reader_.mark);
} }
@ -989,7 +989,7 @@ struct Scanner
scanYAMLDirectiveNumberToSlice(startMark); scanYAMLDirectiveNumberToSlice(startMark);
if(error_) { return; } 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, error("While scanning a directive", startMark,
expected("digit or '.'", reader_.peek()), reader_.mark); expected("digit or '.'", reader_.peek()), reader_.mark);
@ -1063,7 +1063,7 @@ struct Scanner
void scanTagDirectivePrefixToSlice(const Mark startMark) @safe void scanTagDirectivePrefixToSlice(const Mark startMark) @safe
{ {
scanTagURIToSlice!"directive"(startMark); 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, error("While scanning a directive prefix", startMark,
expected("' '", reader_.peek()), reader_.mark); expected("' '", reader_.peek()), reader_.mark);
} }
@ -1110,7 +1110,7 @@ struct Scanner
if(error_) { return Token.init; } if(error_) { return Token.init; }
if(!reader_.peek().isWhiteSpace && if(!reader_.peek().isWhiteSpace &&
!"?:,]}%@"d.canFind(reader_.peekByte())) !reader_.peekByte().among!('?', ':', ',', ']', '}', '%', '@'))
{ {
enum anchorCtx = "While scanning an anchor"; enum anchorCtx = "While scanning an anchor";
enum aliasCtx = "While scanning an alias"; enum aliasCtx = "While scanning an alias";
@ -1257,7 +1257,7 @@ struct Scanner
while(reader_.column == indent && reader_.peekByte() != '\0') while(reader_.column == indent && reader_.peekByte() != '\0')
{ {
breaksTransaction.commit(); 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. // This is where the 'interesting' non-whitespace data gets read.
scanToNextBreakToSlice(); scanToNextBreakToSlice();
lineBreak = scanLineBreak(); lineBreak = scanLineBreak();
@ -1280,7 +1280,7 @@ struct Scanner
// This is the folding according to the specification: // This is the folding according to the specification:
if(style == ScalarStyle.folded && lineBreak == '\n' && 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 // No breaks were scanned; no need to insert the space in the
// middle of slice. // middle of slice.
@ -1373,7 +1373,7 @@ struct Scanner
if(gotIncrement) { getChomping(c, chomping); } 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); return tuple(chomping, increment);
} }
@ -1392,7 +1392,7 @@ struct Scanner
/// chomping = Write the chomping value here, if detected. /// chomping = Write the chomping value here, if detected.
bool getChomping(ref dchar c, ref Chomping chomping) @safe 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; chomping = c == '+' ? Chomping.keep : Chomping.strip;
reader_.forward(); reader_.forward();
c = reader_.peek(); c = reader_.peek();
@ -1455,7 +1455,7 @@ struct Scanner
uint maxIndent; uint maxIndent;
Mark endMark = reader_.mark; 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() != ' ') if(reader_.peekByte() != ' ')
{ {
@ -1481,7 +1481,7 @@ struct Scanner
for(;;) for(;;)
{ {
while(reader_.column < indent && reader_.peekByte() == ' ') { reader_.forward(); } 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()); reader_.sliceBuilder.write(scanLineBreak());
endMark = reader_.mark; endMark = reader_.mark;
} }
@ -1562,7 +1562,7 @@ struct Scanner
reader_.sliceBuilder.write('\''); reader_.sliceBuilder.write('\'');
} }
else if((quotes == ScalarStyle.doubleQuoted && c == '\'') || else if((quotes == ScalarStyle.doubleQuoted && c == '\'') ||
(quotes == ScalarStyle.singleQuoted && "\"\\"d.canFind(c))) (quotes == ScalarStyle.singleQuoted && c.among!('"', '\\')))
{ {
reader_.forward(); reader_.forward();
reader_.sliceBuilder.write(c); reader_.sliceBuilder.write(c);
@ -1571,7 +1571,7 @@ struct Scanner
{ {
reader_.forward(); reader_.forward();
c = reader_.peek(); c = reader_.peek();
if(dyaml.escapes.escapes.canFind(c)) if(c.among!(escapes))
{ {
reader_.forward(); reader_.forward();
// Escaping has been moved to Parser as it can't be done in // 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]; char[2] escapeSequence = ['\\', cast(char)c];
reader_.sliceBuilder.write(escapeSequence); reader_.sliceBuilder.write(escapeSequence);
} }
else if(dyaml.escapes.escapeHexCodeList.canFind(c)) else if(c.among!(escapeHexCodeList))
{ {
const hexLength = dyaml.escapes.escapeHexLength(c); const hexLength = dyaml.escapes.escapeHexLength(c);
reader_.forward(); reader_.forward();
@ -1612,7 +1612,7 @@ struct Scanner
return; return;
} }
} }
else if("\n\r\u0085\u2028\u2029"d.canFind(c)) else if(c.among!('\n', '\r', '\u0085', '\u2028', '\u2029'))
{ {
scanLineBreak(); scanLineBreak();
scanFlowScalarBreaksToSlice(startMark); scanFlowScalarBreaksToSlice(startMark);
@ -1640,7 +1640,7 @@ struct Scanner
{ {
// Increase length as long as we see whitespace. // Increase length as long as we see whitespace.
size_t length; size_t length;
while(" \t"d.canFind(reader_.peekByte(length))) { ++length; } while(reader_.peekByte(length).among!(' ', '\t')) { ++length; }
auto whitespaces = reader_.prefixBytes(length); auto whitespaces = reader_.prefixBytes(length);
// Can check the last byte without striding because '\0' is ASCII // Can check the last byte without striding because '\0' is ASCII
@ -1653,7 +1653,7 @@ struct Scanner
} }
// Spaces not followed by a line break. // 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_.forward(length);
reader_.sliceBuilder.write(whitespaces); reader_.sliceBuilder.write(whitespaces);
@ -1698,10 +1698,10 @@ struct Scanner
} }
// Skip any whitespaces. // 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. // 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(); const lineBreak = scanLineBreak();
anyBreaks = true; anyBreaks = true;
@ -1756,7 +1756,7 @@ struct Scanner
for(;;) for(;;)
{ {
c = reader_.peek(length); c = reader_.peek(length);
if(c.isWhiteSpace || ",:?[]{}"d.canFind(c)) if(c.isWhiteSpace || c.among!(',', ':', '?', '[', ']', '{', '}'))
{ {
break; break;
} }
@ -1767,7 +1767,7 @@ struct Scanner
// It's not clear what we should do with ':' in the flow context. // It's not clear what we should do with ':' in the flow context.
if(flowLevel_ > 0 && c == ':' && if(flowLevel_ > 0 && c == ':' &&
!reader_.peek(length + 1).isWhiteSpace && !reader_.peek(length + 1).isWhiteSpace &&
!",[]{}"d.canFind(reader_.peek(length + 1))) !reader_.peek(length + 1).among!(',', '[', ']', '{', '}'))
{ {
// This is an error; throw the slice away. // This is an error; throw the slice away.
spacesTransaction.commit(); spacesTransaction.commit();
@ -1837,7 +1837,7 @@ struct Scanner
{ {
const prefix = reader_.prefix(3); const prefix = reader_.prefix(3);
return ("---" == prefix || "..." == prefix) 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; } if(end(reader_)) { return; }
@ -1885,7 +1885,7 @@ struct Scanner
c = reader_.peek(length); c = reader_.peek(length);
if(c != ' ') if(c != ' ')
{ {
while(c.isAlphaNum || "-_"d.canFind(c)) while(c.isAlphaNum || c.among!('-', '_'))
{ {
++length; ++length;
c = reader_.peek(length); c = reader_.peek(length);

View file

@ -35,16 +35,16 @@ bool compareEvents(T, U)(T events1, U events2)
//Different event types. //Different event types.
if(e1.id != e2.id){return false;} if(e1.id != e2.id){return false;}
//Different anchor (if applicable). //Different anchor (if applicable).
if([EventID.sequenceStart, if(e1.id.among!(EventID.sequenceStart,
EventID.mappingStart, EventID.mappingStart,
EventID.alias_, EventID.alias_,
EventID.scalar].canFind(e1.id) EventID.scalar)
&& e1.anchor != e2.anchor) && e1.anchor != e2.anchor)
{ {
return false; return false;
} }
//Different collection tag (if applicable). //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; return false;
} }