Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(489)

Side by Side Diff: chrome/browser/history/url_utils.cc

Issue 23477033: Implementing URL prefix match for history thumbnail cache. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Replacing ASSERT_TRUE() << ... to NOTREACHED() << ... Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/history/url_utils.h ('k') | chrome/browser/history/url_utils_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 #include "chrome/browser/history/url_utils.h"
6
7 #include <algorithm>
8
9 namespace history {
10
11 namespace {
12
13 // Comparator to enforce '\0' < '?' < '#' < '/' < other characters.
14 int GetURLCharPriority(char ch) {
15 switch (ch) {
16 case '\0': return 0;
17 case '?': return 1;
18 case '#': return 2;
19 case '/': return 3;
20 }
21 return 4;
22 }
23
24 } // namespace
25
26 // Instead of splitting URLs and extract path components, we can implement
27 // CanonicalURLStringCompare() using string operations only. The key idea is,
28 // treating '/' to be less than any valid path characters would make it behave
29 // as a separator, so e.g., "test" < "test-case" would be enforced by
30 // "test/..." < "test-case/...". We also force "?" < "/", so "test?query" <
31 // "test/stuff". Since the routine is merely lexicographical string comparison
32 // with remapping of chracter ordering, so it is a valid strict-weak ordering.
33 bool CanonicalURLStringCompare(const std::string& s1, const std::string& s2) {
34 const std::string::value_type* ch1 = s1.c_str();
35 const std::string::value_type* ch2 = s2.c_str();
36 while (*ch1 && *ch2 && *ch1 == *ch2) {
37 ++ch1;
38 ++ch2;
39 }
40 int pri_diff = GetURLCharPriority(*ch1) - GetURLCharPriority(*ch2);
41 // We want false to be returned if |pri_diff| > 0.
42 return (pri_diff != 0) ? pri_diff < 0 : *ch1 < *ch2;
43 }
44
45 bool UrlIsPrefix(const GURL& url1, const GURL& url2) {
46 if (url1.scheme() != url2.scheme() || url1.host() != url2.host() ||
47 url1.port() != url2.port()) {
48 return false;
49 }
50 // Only need to compare path now. Note that queries are ignored.
51 std::string p1(url1.path());
52 std::string p2(url2.path());
53 if (p1.length() > p2.length())
54 return false;
55 std::pair<std::string::iterator, std::string::iterator> first_diff =
56 std::mismatch(p1.begin(), p1.end(), p2.begin());
57 // Necessary condition: |p1| is a string prefix of |p2|.
58 if (first_diff.first != p1.end())
59 return false; // E.g.: (|p1| = "/test", |p2| = "/exam") => false.
60
61 // |p1| is string prefix.
62 if (first_diff.second == p2.end()) // Is exact match?
63 return true; // E.g.: ("/test", "/test") => true.
64 // |p1| is strict string prefix, check full match of last path component.
65 if (!p1.empty() && *p1.rbegin() == '/') // Ends in '/'?
66 return true; // E.g.: ("/test/", "/test/stuff") => true.
67
68 // Finally, |p1| does not end in "/": check first extra character in |p2|.
69 // E.g.: ("/test", "/test/stuff") => true; ("/test", "/testing") => false.
70 return *(first_diff.second) == '/';
71 }
72
73 } // namespace history
OLDNEW
« no previous file with comments | « chrome/browser/history/url_utils.h ('k') | chrome/browser/history/url_utils_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698