Skip to content

Commit

Permalink
Fix fromStringOrPath to correctly distinguish (absolute) path and URI
Browse files Browse the repository at this point in the history
  • Loading branch information
jslee02 committed Oct 13, 2015
1 parent c9b21ce commit 78557fd
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
46 changes: 39 additions & 7 deletions dart/common/Uri.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,13 +287,29 @@ bool Uri::fromPath(const std::string& _path)
//==============================================================================
bool Uri::fromStringOrPath(const std::string& _input)
{
// TODO(JS): Need to check if _input is an "absolute" path?

#ifdef _WIN32

// Assume that any URI begin with pattern [SINGLE_LETTER]:[/ or \\] is an
// absolute path.
static regex windowsPathRegex(R"END([a-zA-Z]:[/|\\])END");
bool isPath = regex_search(_input, windowsPathRegex, match_continuous);

if (isPath)
return fromPath(_input);

#else

// Assume that any URI without a scheme is a path.
static regex uriSchemeRegex(R"END(^(([^:/?#]+)://))END");
static regex uriSchemeRegex(R"END(^(([^:/?#]+):))END");
bool noScheme = !regex_search(_input, uriSchemeRegex, match_continuous);

if (noScheme)
return fromPath(_input);

#endif

return fromString(_input);
}

Expand Down Expand Up @@ -431,7 +447,7 @@ Uri Uri::createFromString(const std::string& _input)
<< "'.\n";
}

// We don't need to clear since fromString() does not set any component
// We don't need to clear uri since fromString() does not set any component
// on failure.

return uri;
Expand All @@ -447,12 +463,28 @@ Uri Uri::createFromPath(const std::string& _path)
<< "'.\n";
}

// We don't need to clear since fromString() does not set any component
// We don't need to clear uri since fromString() does not set any component
// on failure.

return fileUri;
}

//==============================================================================
Uri Uri::createFromStringOrPath(const std::string& _input)
{
Uri uri;
if(!uri.fromStringOrPath(_input))
{
dtwarn << "[Uri::createFromString] Failed parsing URI '" << _input
<< "'.\n";
}

// We don't need to clear uri since fromString() does not set any component
// on failure.

return uri;
}

//==============================================================================
Uri Uri::createFromRelativeUri(const std::string& _base,
const std::string& _relative, bool _strict)
Expand All @@ -464,8 +496,8 @@ Uri Uri::createFromRelativeUri(const std::string& _base,
<< "' with base URI '" << _base << "'.\n";
}

// We don't need to clear since fromRelativeUri() does not set any component
// on failure.
// We don't need to clear mergedUri since fromRelativeUri() does not set any
// component on failure.

return mergedUri;
}
Expand All @@ -482,8 +514,8 @@ Uri Uri::createFromRelativeUri(const Uri& _baseUri,
<< _baseUri.toString() << "'.\n";
}

// We don't need to clear since fromRelativeUri() does not set any component
// on failure.
// We don't need to clear mergedUri since fromRelativeUri() does not set any
// component on failure.

return mergedUri;
}
Expand Down
4 changes: 4 additions & 0 deletions dart/common/Uri.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ struct Uri final
/// Create file URI from a string; return an empty URI on failure.
static Uri createFromPath(const std::string& _path);

/// Create general URI or file URI from a string; return an empty URI on
/// failure.
static Uri createFromStringOrPath(const std::string& _input);

/// Create URI resolving a relative path reference; return an empty URI on
/// failure.
static Uri createFromRelativeUri(const std::string& _base,
Expand Down

0 comments on commit 78557fd

Please sign in to comment.