fix build.
[chromium-blink-merge.git] / rlz / lib / string_utils.cc
blob87988022e41ad4ed1e7fd2e25a443ee0c15c613f
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 // String manipulation functions used in the RLZ library.
7 #include "rlz/lib/string_utils.h"
9 #include "rlz/lib/assert.h"
11 namespace rlz_lib {
13 bool IsAscii(unsigned char letter) {
14 return letter < 0x80;
17 bool GetHexValue(char letter, int* value) {
18 if (!value) {
19 ASSERT_STRING("GetHexValue: Invalid output paramter");
20 return false;
22 *value = 0;
24 if (letter >= '0' && letter <= '9')
25 *value = letter - '0';
26 else if (letter >= 'a' && letter <= 'f')
27 *value = (letter - 'a') + 0xA;
28 else if (letter >= 'A' && letter <= 'F')
29 *value = (letter - 'A') + 0xA;
30 else
31 return false;
33 return true;
36 int HexStringToInteger(const char* text) {
37 if (!text) {
38 ASSERT_STRING("HexStringToInteger: text is NULL.");
39 return 0;
42 int idx = 0;
43 // Ignore leading whitespace.
44 while (text[idx] == '\t' || text[idx] == ' ')
45 idx++;
47 if ((text[idx] == '0') &&
48 (text[idx + 1] == 'X' || text[idx + 1] == 'x'))
49 idx +=2; // String is of the form 0x...
51 int number = 0;
52 int digit = 0;
53 for (; text[idx] != '\0'; idx++) {
54 if (!GetHexValue(text[idx], &digit)) {
55 // Ignore trailing whitespaces, but assert on other trailing characters.
56 bool only_whitespaces = true;
57 while (only_whitespaces && text[idx])
58 only_whitespaces = (text[idx++] == ' ');
59 if (!only_whitespaces)
60 ASSERT_STRING("HexStringToInteger: text contains non-hex characters.");
61 return number;
63 number = (number << 4) | digit;
66 return number;
69 bool BytesToString(const unsigned char* data,
70 int data_len,
71 std::string* string) {
72 if (!string)
73 return false;
75 string->clear();
76 if (data_len < 1 || !data)
77 return false;
79 static const char kHex[] = "0123456789ABCDEF";
81 // Fix the buffer size to begin with to avoid repeated re-allocation.
82 string->resize(data_len * 2);
83 int index = data_len;
84 while (index--) {
85 string->at(2 * index) = kHex[data[index] >> 4]; // high digit
86 string->at(2 * index + 1) = kHex[data[index] & 0x0F]; // low digit
89 return true;
92 } // namespace rlz_lib