Home | History | Annotate | Download | only in src
      1 /* Based on nsURLParsers.cc from Mozilla
      2  * -------------------------------------
      3  * Copyright (C) 1998 Netscape Communications Corporation.
      4  *
      5  * Other contributors:
      6  *   Darin Fisher (original author)
      7  *
      8  * This library is free software; you can redistribute it and/or
      9  * modify it under the terms of the GNU Lesser General Public
     10  * License as published by the Free Software Foundation; either
     11  * version 2.1 of the License, or (at your option) any later version.
     12  *
     13  * This library is distributed in the hope that it will be useful,
     14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     16  * Lesser General Public License for more details.
     17  *
     18  * You should have received a copy of the GNU Lesser General Public
     19  * License along with this library; if not, write to the Free Software
     20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
     21  *
     22  * Alternatively, the contents of this file may be used under the terms
     23  * of either the Mozilla Public License Version 1.1, found at
     24  * http://www.mozilla.org/MPL/ (the "MPL") or the GNU General Public
     25  * License Version 2.0, found at http://www.fsf.org/copyleft/gpl.html
     26  * (the "GPL"), in which case the provisions of the MPL or the GPL are
     27  * applicable instead of those above.  If you wish to allow use of your
     28  * version of this file only under the terms of one of those two
     29  * licenses (the MPL or the GPL) and not to allow others to use your
     30  * version of this file under the LGPL, indicate your decision by
     31  * deletingthe provisions above and replace them with the notice and
     32  * other provisions required by the MPL or the GPL, as the case may be.
     33  * If you do not delete the provisions above, a recipient may use your
     34  * version of this file under any of the LGPL, the MPL or the GPL.
     35  */
     36 
     37 #include "config.h"
     38 #include "URLSegments.h"
     39 
     40 namespace WTF {
     41 
     42 int URLSegments::length() const
     43 {
     44     if (fragment.isValid())
     45         return fragment.end();
     46     return charactersBefore(Fragment, false);
     47 }
     48 
     49 int URLSegments::charactersBefore(ComponentType type, bool includeDelimiter) const
     50 {
     51     if (type == Scheme)
     52         return scheme.begin();
     53 
     54     int current = 0;
     55     if (scheme.isValid())
     56         current = scheme.end() + 1; // Advance over the ':' at the end of the scheme.
     57 
     58     if (username.isValid()) {
     59         if (type <= Username)
     60             return username.begin();
     61         current = username.end() + 1; // Advance over the '@' or ':' at the end.
     62     }
     63 
     64     if (password.isValid()) {
     65         if (type <= Password)
     66             return password.begin();
     67         current = password.end() + 1; // Advance over the '@' at the end.
     68     }
     69 
     70     if (host.isValid()) {
     71         if (type <= Host)
     72             return host.begin();
     73         current = host.end();
     74     }
     75 
     76     if (port.isValid()) {
     77         if (type < Port || (type == Port && includeDelimiter))
     78             return port.begin() - 1; // Back over delimiter.
     79         if (type == Port)
     80             return port.begin(); // Don't want delimiter counted.
     81         current = port.end();
     82     }
     83 
     84     if (path.isValid()) {
     85         if (type <= Path)
     86             return path.begin();
     87         current = path.end();
     88     }
     89 
     90     if (query.isValid()) {
     91         if (type < Query || (type == Query && includeDelimiter))
     92             return query.begin() - 1; // Back over delimiter.
     93         if (type == Query)
     94             return query.begin(); // Don't want delimiter counted.
     95         current = query.end();
     96     }
     97 
     98     if (fragment.isValid()) {
     99         if (type == Fragment && !includeDelimiter)
    100             return fragment.begin(); // Back over delimiter.
    101 
    102         // When there is a fragment and we get here, the component we wanted was before
    103         // this and not found, so we always know the beginning of the fragment is right.
    104         return fragment.begin() - 1; // Don't want delimiter counted.
    105     }
    106 
    107     return current;
    108 }
    109 
    110 } // namespace WTF
    111