blob: e0ba8a6e3b6655a2a3a40b35653d5341ae976393 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyevaf283d82014-01-03 13:23:34 -08002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080020 */
21
22#ifndef NDN_STRING_HELPER_HPP
23#define NDN_STRING_HELPER_HPP
24
Alexander Afanasyev8828ca62015-07-02 13:40:09 -070025#include "../common.hpp"
26#include "../encoding/buffer.hpp"
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080027
28namespace ndn {
29
Alexander Afanasyev8828ca62015-07-02 13:40:09 -070030class StringHelperError : public std::invalid_argument
31{
32public:
33 explicit
34 StringHelperError(const std::string& what)
35 : std::invalid_argument(what)
36 {
37 }
38};
39
40/**
41 * @brief Output the hex representation of the bytes in array to the output stream @p os
42 *
43 * @param os Output stream
44 * @param buffer The array of bytes
45 * @param length Size of the array
46 * @param isUpperCase if true (default) output use uppercase for hex values
47 *
48 * @example
49 * printHex(std::cout, "Hello, World!") outputs "48656C6C6F2C20776F726C6421"
50 * printHex(std::cout, "Hello, World!", false) outputs "48656c6c6f2c20776f726c6421"
51 *
52 * Each octet is always represented as two hex characters ("00" for octet==0).
53 *
54 * The output string is a continuous sequence of hex characters without any whitespace separators.
55 */
56void
57printHex(std::ostream& os, const uint8_t* buffer, size_t length, bool isUpperCase = true);
58
59/**
60 * @brief Output the hex representation of the bytes in the @p buffer to the output stream @p os
61 *
62 * @param os Output stream
63 * @param buffer The array of bytes
64 * @param isUpperCase if true (default) output use uppercase for hex values
65 */
66void
67printHex(std::ostream& os, const Buffer& buffer, bool isUpperCase = true);
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080068
69/**
Alexander Afanasyev770827c2014-05-13 17:42:55 -070070 * @brief Return the hex representation of the bytes in array
71 *
Alexander Afanasyev8828ca62015-07-02 13:40:09 -070072 * @param buffer The array of bytes
73 * @param length Size of the array
74 * @param isUpperCase if true (default) output use uppercase for hex values
75 *
76 * @example
77 * toHex("Hello, World!") == "48656C6C6F2C20776F726C6421"
78 * toHex("Hello, World!", false) == "48656c6c6f2c20776f726c6421"
79 *
80 * Each octet is always represented as two hex characters ("00" for octet==0).
81 *
82 * The output string is a continuous sequence of hex characters without any whitespace separators.
Alexander Afanasyev809805d2014-02-17 17:20:33 -080083 */
Alexander Afanasyev8828ca62015-07-02 13:40:09 -070084std::string
85toHex(const uint8_t* buffer, size_t length, bool isUpperCase = true);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070086
Alexander Afanasyev8828ca62015-07-02 13:40:09 -070087/**
88 * @brief Return the hex representation of the bytes in the @p buffer to the output stream @p os
89 *
90 * @param buffer The array of bytes
91 * @param isUpperCase if true (default) output use uppercase for hex values
92 */
93std::string
94toHex(const Buffer& buffer, bool isUpperCase = true);
Alexander Afanasyev809805d2014-02-17 17:20:33 -080095
Alexander Afanasyev8828ca62015-07-02 13:40:09 -070096/**
97 * @brief Convert the hex string to buffer
98 * @param hexString sequence of pairs of hex numbers (lower and upper case can be mixed)
99 * without any whitespace separators (e.g., "48656C6C6F2C20776F726C6421")
100 * @throw StringHelperError if input is invalid
101 */
102shared_ptr<const Buffer>
103fromHex(const std::string& hexString);
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800104
105/**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700106 * @brief Modify str in place to erase whitespace on the left
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800107 */
Alexander Afanasyev8828ca62015-07-02 13:40:09 -0700108void
109trimLeft(std::string& str);
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800110
111/**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700112 * @brief Modify str in place to erase whitespace on the right
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800113 */
Alexander Afanasyev8828ca62015-07-02 13:40:09 -0700114void
115trimRight(std::string& str);
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800116
117/**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700118 * @brief Modify str in place to erase whitespace on the left and right
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800119 */
Alexander Afanasyev8828ca62015-07-02 13:40:09 -0700120void
121trim(std::string& str);
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800122
123/**
Alexander Afanasyev770827c2014-05-13 17:42:55 -0700124 * @brief Convert the hex character to an integer from 0 to 15, or -1 if not a hex character
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800125 */
Alexander Afanasyev8828ca62015-07-02 13:40:09 -0700126int
127fromHexChar(uint8_t c);
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800128
129/**
Alexander Afanasyev8828ca62015-07-02 13:40:09 -0700130 * @brief Decode a percent-encoded string
131 * @see RFC 3986 section 2
132 *
133 * When % is not followed by two hex characters, the output is not transformed.
134 *
135 * @example unescape("hello%20world") == "hello world"
136 * @example unescape("hello%20world%FooBar") == "hello world%FooBar"
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800137 */
Alexander Afanasyev8828ca62015-07-02 13:40:09 -0700138std::string
139unescape(const std::string& str);
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800140
141} // namespace ndn
142
143#endif // NDN_STRING_HELPER_HPP