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

Side by Side Diff: components/physical_web/eddystone/eddystone_encoder.cc

Issue 2731273004: Add Platform Independent Eddystone Encoder (Closed)
Patch Set: Created 3 years, 9 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
OLDNEW
(Empty)
1 // Copyright 2017 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 "components/physical_web/eddystone/eddystone_encoder.h"
6
7 #include <string>
8 #include <vector>
9
10 #include "url/gurl.h"
11
12 namespace {
13 const char* kPrefixes[] = {"http://www.", "https://www.", "http://",
14 "https://"};
15 const size_t kNumPrefixes = sizeof(kPrefixes) / sizeof(char*);
16
17 const char* kSuffixes[] = {".com/", ".org/", ".edu/", ".net/", ".info/",
18 ".biz/", ".gov/", ".com", ".org", ".edu",
19 ".net", ".info", ".biz", ".gov"};
20 const size_t kNumSuffixes = sizeof(kSuffixes) / sizeof(char*);
21 }
22
23 namespace physical_web {
24 std::vector<uint8_t> EncodeUrl(std::string c_url) {
mattreynolds 2017/03/08 19:14:46 Use a const reference for the input parameter to a
iankc 2017/03/09 20:45:44 Done.
25 std::vector<uint8_t> encodedUrl;
mattreynolds 2017/03/08 19:14:46 Use snake_case for common variables
iankc 2017/03/09 20:45:45 Done.
26
27 if (c_url.empty()) {
28 encodedUrl.push_back(-1);
mattreynolds 2017/03/08 19:14:46 Returning errors by setting invalid values in the
iankc 2017/03/09 20:45:44 Done.
29 return encodedUrl;
30 }
31
32 GURL gurl(c_url);
33 if (!gurl.is_valid()) {
34 encodedUrl.push_back(-2);
35 return encodedUrl;
36 }
37
38 if (gurl.HostIsIPAddress() || !gurl.SchemeIsHTTPOrHTTPS()) {
39 encodedUrl.push_back(-3);
40 return encodedUrl;
41 }
42
43 /*
44 * Necessary Declarations
45 */
46 const char* raw_url_position = c_url.c_str();
47
48 size_t prefixLengths[kNumPrefixes] = {};
mattreynolds 2017/03/08 19:14:46 Can you make this static? Then we can compute it
iankc 2017/03/09 20:45:45 Done.
49 // Compute once because strlen is expensive.
50 for (size_t i = 0; i < kNumPrefixes; i++) {
51 prefixLengths[i] = strlen(kPrefixes[i]);
52 }
53
54 size_t suffixLengths[kNumSuffixes] = {};
55 for (size_t i = 0; i < kNumSuffixes; i++) {
56 suffixLengths[i] = strlen(kSuffixes[i]);
57 }
58 /*
59 * handle prefix
60 */
61 for (size_t i = 0; i < kNumPrefixes; i++) {
62 size_t prefixLen = prefixLengths[i];
63 if (strncmp(raw_url_position, kPrefixes[i], prefixLen) == 0) {
64 encodedUrl.push_back(i);
65 raw_url_position += prefixLen;
66 break;
67 }
68 }
69
70 /*
71 * handle suffixes
72 */
73 while (*raw_url_position) {
74 /* check for suffix match */
75 size_t i;
76 for (i = 0; i < kNumSuffixes; i++) {
77 size_t suffixLen = suffixLengths[i];
78 if (strncmp(raw_url_position, kSuffixes[i], suffixLen) == 0) {
79 encodedUrl.push_back(i);
80 raw_url_position += suffixLen;
81 break; /* from the for loop for checking against suffixes */
82 }
83 }
84 /* This is the default case where we've got an ordinary character which
85 * doesn't match a suffix. */
86 if (i == kNumSuffixes) {
87 encodedUrl.push_back(*raw_url_position++);
88 }
89 }
90
91 return encodedUrl;
92 }
93 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698