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
5#ifndef URL_URL_PARSE_INTERNAL_H_
6#define URL_URL_PARSE_INTERNAL_H_
7
8// Contains common inline helper functions used by the URL parsing routines.
9
10#include "url/url_parse.h"
11
12namespace url {
13
14// We treat slashes and backslashes the same for IE compatability.
15inline bool IsURLSlash(base::char16 ch) {
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.
21inline bool ShouldTrimFromURL(base::char16 ch) {
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|).
30template<typename CHAR>
31inline void TrimURL(const CHAR* spec, int* begin, int* len,
32                    bool trim_path_end = true) {
33  // Strip leading whitespace and control characters.
34  while (*begin < *len && ShouldTrimFromURL(spec[*begin]))
35    (*begin)++;
36
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  }
44}
45
46// Counts the number of consecutive slashes starting at the given offset
47// in the given string of the given length.
48template<typename CHAR>
49inline 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.
65void ParsePathInternal(const char* spec,
66                       const Component& path,
67                       Component* filepath,
68                       Component* query,
69                       Component* ref);
70void ParsePathInternal(const base::char16* spec,
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.
80void ParseAfterScheme(const char* spec,
81                      int spec_len,
82                      int after_scheme,
83                      Parsed* parsed);
84void ParseAfterScheme(const base::char16* spec,
85                      int spec_len,
86                      int after_scheme,
87                      Parsed* parsed);
88
89}  // namespace url
90
91#endif  // URL_URL_PARSE_INTERNAL_H_
92