1ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// Copyright (c) 2011 The Chromium Authors. All rights reserved.
23345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// Use of this source code is governed by a BSD-style license that can be
33345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// found in the LICENSE file.
43345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
53345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#ifndef BASE_STRING_NUMBER_CONVERSIONS_H_
63345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#define BASE_STRING_NUMBER_CONVERSIONS_H_
73345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
83345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#include <string>
93345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#include <vector>
103345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
11ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "base/base_api.h"
123345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#include "base/basictypes.h"
133345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#include "base/string16.h"
143345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
153345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// ----------------------------------------------------------------------------
163345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// IMPORTANT MESSAGE FROM YOUR SPONSOR
173345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick//
183345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// This file contains no "wstring" variants. New code should use string16. If
193345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// you need to make old code work, use the UTF8 version and convert. Please do
203345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// not add wstring variants.
213345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick//
223345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// Please do not add "convenience" functions for converting strings to integers
233345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// that return the value and ignore success/failure. That encourages people to
243345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// write code that doesn't properly handle the error conditions.
253345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// ----------------------------------------------------------------------------
263345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
273345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merricknamespace base {
283345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
293345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// Number -> string conversions ------------------------------------------------
303345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
31ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API std::string IntToString(int value);
32ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API string16 IntToString16(int value);
333345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
34ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API std::string UintToString(unsigned value);
35ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API string16 UintToString16(unsigned value);
363345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
37ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API std::string Int64ToString(int64 value);
38ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API string16 Int64ToString16(int64 value);
393345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
40ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API std::string Uint64ToString(uint64 value);
41ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API string16 Uint64ToString16(uint64 value);
423345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
433345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// DoubleToString converts the double to a string format that ignores the
443345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// locale. If you want to use locale specific formatting, use ICU.
45ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API std::string DoubleToString(double value);
463345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
473345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// String -> number conversions ------------------------------------------------
483345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
493345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// Perform a best-effort conversion of the input string to a numeric type,
503345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// setting |*output| to the result of the conversion.  Returns true for
513345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// "perfect" conversions; returns false in the following cases:
523345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick//  - Overflow/underflow.  |*output| will be set to the maximum value supported
533345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick//    by the data type.
543345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick//  - Trailing characters in the string after parsing the number.  |*output|
553345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick//    will be set to the value of the number that was parsed.
56731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick//  - Leading whitespace in the string before parsing the number. |*output| will
57731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick//    be set to the value of the number that was parsed.
583345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick//  - No characters parseable as a number at the beginning of the string.
593345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick//    |*output| will be set to 0.
603345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick//  - Empty string.  |*output| will be set to 0.
61ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool StringToInt(const std::string& input, int* output);
62ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool StringToInt(std::string::const_iterator begin,
63ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                          std::string::const_iterator end,
64ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                          int* output);
65ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool StringToInt(const char* begin, const char* end, int* output);
66ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
67ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool StringToInt(const string16& input, int* output);
68ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool StringToInt(string16::const_iterator begin,
69ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                          string16::const_iterator end,
70ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                          int* output);
71ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool StringToInt(const char16* begin, const char16* end, int* output);
72ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
73ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool StringToInt64(const std::string& input, int64* output);
74ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool StringToInt64(std::string::const_iterator begin,
75ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                            std::string::const_iterator end,
76ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                            int64* output);
77ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool StringToInt64(const char* begin, const char* end, int64* output);
78ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
79ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool StringToInt64(const string16& input, int64* output);
80ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool StringToInt64(string16::const_iterator begin,
81ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                            string16::const_iterator end,
82ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                            int64* output);
83ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool StringToInt64(const char16* begin, const char16* end,
84ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                            int64* output);
853345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
863345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// For floating-point conversions, only conversions of input strings in decimal
873345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// form are defined to work.  Behavior with strings representing floating-point
883345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// numbers in hexadecimal, and strings representing non-fininte values (such as
893345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// NaN and inf) is undefined.  Otherwise, these behave the same as the integral
903345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// variants.  This expects the input string to NOT be specific to the locale.
913345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// If your input is locale specific, use ICU to read the number.
92ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool StringToDouble(const std::string& input, double* output);
933345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
943345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// Hex encoding ----------------------------------------------------------------
953345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
963345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// Returns a hex string representation of a binary buffer. The returned hex
973345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// string will be in upper case. This function does not check if |size| is
983345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// within reasonable limits since it's written with trusted data in mind.  If
993345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// you suspect that the data you want to format might be large, the absolute
1003345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// max size for |size| should be is
1013345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick//   std::numeric_limits<size_t>::max() / 2
102ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API std::string HexEncode(const void* bytes, size_t size);
1033345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
1043345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// Best effort conversion, see StringToInt above for restrictions.
105ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool HexStringToInt(const std::string& input, int* output);
106ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool HexStringToInt(std::string::const_iterator begin,
107ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                             std::string::const_iterator end,
108ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                             int* output);
109ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool HexStringToInt(const char* begin, const char* end, int* output);
1103345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
1113345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// Similar to the previous functions, except that output is a vector of bytes.
1123345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// |*output| will contain as many bytes as were successfully parsed prior to the
1133345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// error.  There is no overflow, but input.size() must be evenly divisible by 2.
1143345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// Leading 0x or +/- are not allowed.
115ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian MonsenBASE_API bool HexStringToBytes(const std::string& input,
116ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen                               std::vector<uint8>* output);
1173345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
1183345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick}  // namespace base
1193345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
1203345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#endif  // BASE_STRING_NUMBER_CONVERSIONS_H_
1213345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick
122