[email protected] | 51bcc5d | 2013-04-24 01:41:37 | [diff] [blame] | 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. |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 4 | |
[email protected] | 318076b | 2013-04-18 21:19:45 | [diff] [blame] | 5 | #ifndef URL_URL_PARSE_INTERNAL_H_ |
| 6 | #define URL_URL_PARSE_INTERNAL_H_ |
| 7 | |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 8 | // Contains common inline helper functions used by the URL parsing routines. |
| 9 | |
tfarina | 018de6e | 2015-05-26 17:41:20 | [diff] [blame] | 10 | #include "url/third_party/mozilla/url_parse.h" |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 11 | |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 12 | namespace url { |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 13 | |
qyearsley | 2bc727d | 2015-08-14 20:17:15 | [diff] [blame] | 14 | // We treat slashes and backslashes the same for IE compatibility. |
[email protected] | 3774f83 | 2013-06-11 21:21:57 | [diff] [blame] | 15 | inline bool IsURLSlash(base::char16 ch) { |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 16 | return ch == '/' || ch == '\\'; |
| 17 | } |
| 18 | |
| 19 | // Returns true if we should trim this character from the URL because it is a |
| 20 | // space or a control character. |
[email protected] | 3774f83 | 2013-06-11 21:21:57 | [diff] [blame] | 21 | inline bool ShouldTrimFromURL(base::char16 ch) { |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 22 | return ch <= ' '; |
| 23 | } |
| 24 | |
| 25 | // Given an already-initialized begin index and length, this shrinks the range |
| 26 | // to eliminate "should-be-trimmed" characters. Note that the length does *not* |
| 27 | // indicate the length of untrimmed data from |*begin|, but rather the position |
| 28 | // in the input string (so the string starts at character |*begin| in the spec, |
| 29 | // and goes until |*len|). |
| 30 | template<typename CHAR> |
[email protected] | 369e84f7 | 2013-11-23 01:53:52 | [diff] [blame] | 31 | inline void TrimURL(const CHAR* spec, int* begin, int* len, |
| 32 | bool trim_path_end = true) { |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 33 | // Strip leading whitespace and control characters. |
| 34 | while (*begin < *len && ShouldTrimFromURL(spec[*begin])) |
| 35 | (*begin)++; |
| 36 | |
[email protected] | 369e84f7 | 2013-11-23 01:53:52 | [diff] [blame] | 37 | if (trim_path_end) { |
| 38 | // Strip trailing whitespace and control characters. We need the >i test |
| 39 | // for when the input string is all blanks; we don't want to back past the |
| 40 | // input. |
| 41 | while (*len > *begin && ShouldTrimFromURL(spec[*len - 1])) |
| 42 | (*len)--; |
| 43 | } |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | // Counts the number of consecutive slashes starting at the given offset |
| 47 | // in the given string of the given length. |
| 48 | template<typename CHAR> |
| 49 | inline int CountConsecutiveSlashes(const CHAR *str, |
| 50 | int begin_offset, int str_len) { |
| 51 | int count = 0; |
| 52 | while (begin_offset + count < str_len && |
| 53 | IsURLSlash(str[begin_offset + count])) |
| 54 | ++count; |
| 55 | return count; |
| 56 | } |
| 57 | |
| 58 | // Internal functions in url_parse.cc that parse the path, that is, everything |
| 59 | // following the authority section. The input is the range of everything |
| 60 | // following the authority section, and the output is the identified ranges. |
| 61 | // |
| 62 | // This is designed for the file URL parser or other consumers who may do |
| 63 | // special stuff at the beginning, but want regular path parsing, it just |
| 64 | // maps to the internal parsing function for paths. |
| 65 | void ParsePathInternal(const char* spec, |
| 66 | const Component& path, |
| 67 | Component* filepath, |
| 68 | Component* query, |
| 69 | Component* ref); |
[email protected] | 3774f83 | 2013-06-11 21:21:57 | [diff] [blame] | 70 | void ParsePathInternal(const base::char16* spec, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 71 | const Component& path, |
| 72 | Component* filepath, |
| 73 | Component* query, |
| 74 | Component* ref); |
| 75 | |
| 76 | |
| 77 | // Given a spec and a pointer to the character after the colon following the |
| 78 | // scheme, this parses it and fills in the structure, Every item in the parsed |
| 79 | // structure is filled EXCEPT for the scheme, which is untouched. |
| 80 | void ParseAfterScheme(const char* spec, |
| 81 | int spec_len, |
| 82 | int after_scheme, |
| 83 | Parsed* parsed); |
[email protected] | 3774f83 | 2013-06-11 21:21:57 | [diff] [blame] | 84 | void ParseAfterScheme(const base::char16* spec, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 85 | int spec_len, |
| 86 | int after_scheme, |
| 87 | Parsed* parsed); |
| 88 | |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 89 | } // namespace url |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 90 | |
[email protected] | 318076b | 2013-04-18 21:19:45 | [diff] [blame] | 91 | #endif // URL_URL_PARSE_INTERNAL_H_ |