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

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

Issue 2731273004: Add Platform Independent Eddystone Encoder (Closed)
Patch Set: Fixing more Casting 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 size_t EncodeUrl(const std::string& url, std::vector<uint8_t>* encoded_url) {
25 /*
26 * Sanitize input.
27 */
28
29 if (encoded_url == NULL)
30 return kNullEncodedUrl;
31 if (url.empty())
32 return kEmptyUrl;
33
34 GURL gurl(url);
35 if (!gurl.is_valid())
36 return kInvalidUrl;
37 if (gurl.HostIsIPAddress() || !gurl.SchemeIsHTTPOrHTTPS())
38 return kInvalidFormat;
39
40 /*
41 * Necessary Declarations.
42 */
43 const char* raw_url_position = url.c_str();
44
45 size_t prefix_lengths[kNumPrefixes];
46 size_t suffix_lengths[kNumSuffixes];
47 for (size_t i = 0; i < kNumPrefixes; i++) {
48 prefix_lengths[i] = strlen(kPrefixes[i]);
49 }
50 for (size_t i = 0; i < kNumSuffixes; i++) {
51 suffix_lengths[i] = strlen(kSuffixes[i]);
52 }
53
54 /*
55 * Handle prefix.
56 */
57 for (size_t i = 0; i < kNumPrefixes; i++) {
58 size_t prefix_len = prefix_lengths[i];
59 if (strncmp(raw_url_position, kPrefixes[i], prefix_len) == 0) {
60 encoded_url->push_back(static_cast<uint8_t>(i));
61 raw_url_position += prefix_len;
62 break;
63 }
64 }
65
66 /*
67 * Handle suffixes.
68 */
69 while (*raw_url_position) {
70 /* check for suffix match */
71 size_t i;
72 for (i = 0; i < kNumSuffixes; i++) {
73 size_t suffix_len = suffix_lengths[i];
74 if (strncmp(raw_url_position, kSuffixes[i], suffix_len) == 0) {
75 encoded_url->push_back(static_cast<uint8_t>(i));
76 raw_url_position += suffix_len;
77 break; /* from the for loop for checking against suffixes */
78 }
79 }
80 /* This is the default case where we've got an ordinary character which
81 * doesn't match a suffix. */
82 if (i == kNumSuffixes) {
83 encoded_url->push_back(*raw_url_position);
84 raw_url_position++;
85 }
86 }
87
88 return encoded_url->size();
89 }
90 }
OLDNEW
« no previous file with comments | « components/physical_web/eddystone/eddystone_encoder.h ('k') | components/physical_web/eddystone/eddystone_encoder_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698