url_file.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef URL_URL_FILE_H_
  5. #define URL_URL_FILE_H_
  6. // Provides shared functions used by the internals of the parser and
  7. // canonicalizer for file URLs. Do not use outside of these modules.
  8. #include "base/strings/string_util.h"
  9. #include "url/url_parse_internal.h"
  10. namespace url {
  11. // We allow both "c:" and "c|" as drive identifiers.
  12. inline bool IsWindowsDriveSeparator(char16_t ch) {
  13. return ch == ':' || ch == '|';
  14. }
  15. inline bool IsWindowsDriveSeparator(char ch) {
  16. return IsWindowsDriveSeparator(static_cast<char16_t>(ch));
  17. }
  18. // Returns the index of the next slash in the input after the given index, or
  19. // spec_len if the end of the input is reached.
  20. template<typename CHAR>
  21. inline int FindNextSlash(const CHAR* spec, int begin_index, int spec_len) {
  22. int idx = begin_index;
  23. while (idx < spec_len && !IsURLSlash(spec[idx]))
  24. idx++;
  25. return idx;
  26. }
  27. // DoesContainWindowsDriveSpecUntil returns the least number between
  28. // start_offset and max_offset such that the spec has a valid drive
  29. // specification starting at that offset. Otherwise it returns -1. This function
  30. // gracefully handles, by returning -1, start_offset values that are equal to or
  31. // larger than the spec_len, and caps max_offset appropriately to simplify
  32. // callers. max_offset must be at least start_offset.
  33. template <typename CHAR>
  34. inline int DoesContainWindowsDriveSpecUntil(const CHAR* spec,
  35. int start_offset,
  36. int max_offset,
  37. int spec_len) {
  38. CHECK_LE(start_offset, max_offset);
  39. if (start_offset > spec_len - 2)
  40. return -1; // Not enough room.
  41. if (max_offset > spec_len - 2)
  42. max_offset = spec_len - 2;
  43. for (int offset = start_offset; offset <= max_offset; ++offset) {
  44. if (!base::IsAsciiAlpha(spec[offset]))
  45. continue; // Doesn't contain a valid drive letter.
  46. if (!IsWindowsDriveSeparator(spec[offset + 1]))
  47. continue; // Isn't followed with a drive separator.
  48. return offset;
  49. }
  50. return -1;
  51. }
  52. // Returns true if the start_offset in the given spec looks like it begins a
  53. // drive spec, for example "c:". This function explicitly handles start_offset
  54. // values that are equal to or larger than the spec_len to simplify callers.
  55. //
  56. // If this returns true, the spec is guaranteed to have a valid drive letter
  57. // plus a drive letter separator (a colon or a pipe) starting at |start_offset|.
  58. template <typename CHAR>
  59. inline bool DoesBeginWindowsDriveSpec(const CHAR* spec,
  60. int start_offset,
  61. int spec_len) {
  62. return DoesContainWindowsDriveSpecUntil(spec, start_offset, start_offset,
  63. spec_len) == start_offset;
  64. }
  65. #ifdef WIN32
  66. // Returns true if the start_offset in the given text looks like it begins a
  67. // UNC path, for example "\\". This function explicitly handles start_offset
  68. // values that are equal to or larger than the spec_len to simplify callers.
  69. //
  70. // When strict_slashes is set, this function will only accept backslashes as is
  71. // standard for Windows. Otherwise, it will accept forward slashes as well
  72. // which we use for a lot of URL handling.
  73. template<typename CHAR>
  74. inline bool DoesBeginUNCPath(const CHAR* text,
  75. int start_offset,
  76. int len,
  77. bool strict_slashes) {
  78. int remaining_len = len - start_offset;
  79. if (remaining_len < 2)
  80. return false;
  81. if (strict_slashes)
  82. return text[start_offset] == '\\' && text[start_offset + 1] == '\\';
  83. return IsURLSlash(text[start_offset]) && IsURLSlash(text[start_offset + 1]);
  84. }
  85. #endif // WIN32
  86. } // namespace url
  87. #endif // URL_URL_FILE_H_