Home | History | Annotate | Download | only in url
      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 
     12 namespace url_parse {
     13 
     14 // We treat slashes and backslashes the same for IE compatability.
     15 inline 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.
     21 inline 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|).
     30 template<typename CHAR>
     31 inline void TrimURL(const CHAR* spec, int* begin, int* len) {
     32   // Strip leading whitespace and control characters.
     33   while (*begin < *len && ShouldTrimFromURL(spec[*begin]))
     34     (*begin)++;
     35 
     36   // Strip trailing whitespace and control characters. We need the >i test for
     37   // when the input string is all blanks; we don't want to back past the input.
     38   while (*len > *begin && ShouldTrimFromURL(spec[*len - 1]))
     39     (*len)--;
     40 }
     41 
     42 // Counts the number of consecutive slashes starting at the given offset
     43 // in the given string of the given length.
     44 template<typename CHAR>
     45 inline int CountConsecutiveSlashes(const CHAR *str,
     46                                    int begin_offset, int str_len) {
     47   int count = 0;
     48   while (begin_offset + count < str_len &&
     49          IsURLSlash(str[begin_offset + count]))
     50     ++count;
     51   return count;
     52 }
     53 
     54 // Internal functions in url_parse.cc that parse the path, that is, everything
     55 // following the authority section. The input is the range of everything
     56 // following the authority section, and the output is the identified ranges.
     57 //
     58 // This is designed for the file URL parser or other consumers who may do
     59 // special stuff at the beginning, but want regular path parsing, it just
     60 // maps to the internal parsing function for paths.
     61 void ParsePathInternal(const char* spec,
     62                        const Component& path,
     63                        Component* filepath,
     64                        Component* query,
     65                        Component* ref);
     66 void ParsePathInternal(const base::char16* spec,
     67                        const Component& path,
     68                        Component* filepath,
     69                        Component* query,
     70                        Component* ref);
     71 
     72 
     73 // Given a spec and a pointer to the character after the colon following the
     74 // scheme, this parses it and fills in the structure, Every item in the parsed
     75 // structure is filled EXCEPT for the scheme, which is untouched.
     76 void ParseAfterScheme(const char* spec,
     77                       int spec_len,
     78                       int after_scheme,
     79                       Parsed* parsed);
     80 void ParseAfterScheme(const base::char16* spec,
     81                       int spec_len,
     82                       int after_scheme,
     83                       Parsed* parsed);
     84 
     85 }  // namespace url_parse
     86 
     87 #endif  // URL_URL_PARSE_INTERNAL_H_
     88