diff options
author | Jonathan Hseu <jhseu@google.com> | 2016-09-22 14:41:03 -0800 |
---|---|---|
committer | TensorFlower Gardener <gardener@tensorflow.org> | 2016-09-22 15:48:12 -0700 |
commit | 929160abd520849f0280c7804a7915699ba3f547 (patch) | |
tree | 90ff6b0d3b62a264be51960c74b8cbb0ca029673 /tensorflow/core/platform/file_system.cc | |
parent | f222548518eb8bebdfdc0a7f8941b9c594f6b4f8 (diff) |
Add a ParseURI function and remove the other URI parsing functions.
ParseURI provides all the functionality used by other filesystems right now, so we're able to remove their custom URI parsing code.
- Use ParseURI for URI parsing in GCS and HDFS.
- Note that this fixes a bug where FileSystem::TranslateName("file://foo/bar")
gives "foo/bar" as the path. "file:///" is the correct prefix in that case.
Change: 134009580
Diffstat (limited to 'tensorflow/core/platform/file_system.cc')
-rw-r--r-- | tensorflow/core/platform/file_system.cc | 47 |
1 files changed, 23 insertions, 24 deletions
diff --git a/tensorflow/core/platform/file_system.cc b/tensorflow/core/platform/file_system.cc index e168101a05..928bf644f4 100644 --- a/tensorflow/core/platform/file_system.cc +++ b/tensorflow/core/platform/file_system.cc @@ -51,35 +51,34 @@ WritableFile::~WritableFile() {} FileSystemRegistry::~FileSystemRegistry() {} -string GetSchemeFromURI(const string& name) { - auto colon_loc = name.find(":"); +void ParseURI(StringPiece remaining, StringPiece* scheme, StringPiece* host, + StringPiece* path) { + // 0. Parse scheme // Make sure scheme matches [a-zA-Z][0-9a-zA-Z.]* // TODO(keveman): Allow "+" and "-" in the scheme. - if (colon_loc != string::npos && - strings::Scanner(StringPiece(name.data(), colon_loc)) - .One(strings::Scanner::LETTER) - .Many(strings::Scanner::LETTER_DIGIT_DOT) - .GetResult()) { - return name.substr(0, colon_loc); + if (!strings::Scanner(remaining) + .One(strings::Scanner::LETTER) + .Many(strings::Scanner::LETTER_DIGIT_DOT) + .StopCapture() + .OneLiteral("://") + .GetResult(&remaining, scheme)) { + // If there's no scheme, assume the entire string is a path. + scheme->clear(); + host->clear(); + *path = remaining; + return; } - return ""; -} -string GetNameFromURI(const string& name) { - string scheme = GetSchemeFromURI(name); - if (scheme == "") { - return name; - } - // Skip the 'scheme:' portion. - StringPiece filename{name.data() + scheme.length() + 1, - name.length() - scheme.length() - 1}; - // If the URI confirmed to scheme://filename, skip the two '/'s and return - // filename. Otherwise return the original 'name', and leave it up to the - // implementations to handle the full URI. - if (filename.Consume("//")) { - return filename.ToString(); + // 1. Parse host + if (!strings::Scanner(remaining).ScanUntil('/').GetResult(&remaining, host)) { + // No path, so the rest of the URI is the host. + *host = remaining; + path->clear(); + return; } - return name; + + // 2. The rest is the path + *path = remaining; } } // namespace tensorflow |