public static bool TryParse(string data, out FileLine fileLine) { fileLine = None; if (string.IsNullOrEmpty(data)) { return(false); } var parts = data.Split('.'); if (parts.Length != 2) { return(false); } if (!int.TryParse(parts[0], out var number)) { return(false); } var name = parts[1].TrimStart(); if (string.IsNullOrEmpty(name)) { return(false); } fileLine = new FileLine(number, name, sizeof(int) + name.Length); return(true); }
public static bool TryParse(StreamReader streamReader, out FileLine fileLine) { fileLine = None; if (streamReader.EndOfStream) { return(false); } while (!streamReader.EndOfStream && char.IsWhiteSpace((char)streamReader.Peek())) { streamReader.Read(); } char current; int size = 0; int number = 0; while (!streamReader.EndOfStream && (current = (char)streamReader.Read()) != '.' && size <= _maxNumberLength) { if (!char.IsNumber(current)) { return(false); } int numberPart = current - '0'; if (number == _maxNumberDivTen && numberPart > _maxNumberModTen) { return(false); } number = number * 10 + numberPart; size++; } if (streamReader.EndOfStream) { return(false); } while (!streamReader.EndOfStream && char.IsWhiteSpace((char)streamReader.Peek())) { streamReader.Read(); } var name = streamReader.ReadLine(); if (string.IsNullOrEmpty(name)) { return(false); } fileLine = new FileLine(number, name, sizeof(int) + name.Length); return(true); }