blob: 3f1a303482254d53e8f12deffc5f0ffcc5c49043 [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/**
Davide Pesavento03115ce2017-01-27 01:17:55 -05003 * Copyright (c) 2013-2017 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
Davide Pesavento03115ce2017-01-27 01:17:55 -050022#ifndef NDN_UTIL_STRING_HELPER_HPP
23#define NDN_UTIL_STRING_HELPER_HPP
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080024
Alexander Afanasyev8828ca62015-07-02 13:40:09 -070025#include "../common.hpp"
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080026
27namespace ndn {
28
Davide Pesaventodd461432017-01-28 21:47:26 -050029class Buffer;
30
Alexander Afanasyev8828ca62015-07-02 13:40:09 -070031class StringHelperError : public std::invalid_argument
32{
33public:
34 explicit
35 StringHelperError(const std::string& what)
36 : std::invalid_argument(what)
37 {
38 }
39};
40
41/**
42 * @brief Output the hex representation of the bytes in array to the output stream @p os
43 *
44 * @param os Output stream
45 * @param buffer The array of bytes
46 * @param length Size of the array
Davide Pesaventofdda2412017-01-28 18:53:51 -050047 * @param wantUpperCase if true (default) output use uppercase for hex values
Alexander Afanasyev8828ca62015-07-02 13:40:09 -070048 *
philo7b187f52015-08-24 14:07:08 -070049 * Examples:
50 *
Alexander Afanasyev8828ca62015-07-02 13:40:09 -070051 * printHex(std::cout, "Hello, World!") outputs "48656C6C6F2C20776F726C6421"
52 * printHex(std::cout, "Hello, World!", false) outputs "48656c6c6f2c20776f726c6421"
53 *
54 * Each octet is always represented as two hex characters ("00" for octet==0).
55 *
56 * The output string is a continuous sequence of hex characters without any whitespace separators.
57 */
58void
Davide Pesaventofdda2412017-01-28 18:53:51 -050059printHex(std::ostream& os, const uint8_t* buffer, size_t length, bool wantUpperCase = true);
Alexander Afanasyev8828ca62015-07-02 13:40:09 -070060
61/**
62 * @brief Output the hex representation of the bytes in the @p buffer to the output stream @p os
63 *
64 * @param os Output stream
65 * @param buffer The array of bytes
Davide Pesaventofdda2412017-01-28 18:53:51 -050066 * @param wantUpperCase if true (default) output use uppercase for hex values
Alexander Afanasyev8828ca62015-07-02 13:40:09 -070067 */
68void
Davide Pesaventofdda2412017-01-28 18:53:51 -050069printHex(std::ostream& os, const Buffer& buffer, bool wantUpperCase = true);
Alexander Afanasyevaf283d82014-01-03 13:23:34 -080070
71/**
Alexander Afanasyev770827c2014-05-13 17:42:55 -070072 * @brief Return the hex representation of the bytes in array
73 *
Alexander Afanasyev8828ca62015-07-02 13:40:09 -070074 * @param buffer The array of bytes
75 * @param length Size of the array
Davide Pesaventofdda2412017-01-28 18:53:51 -050076 * @param wantUpperCase if true (default) output use uppercase for hex values
Alexander Afanasyev8828ca62015-07-02 13:40:09 -070077 *
philo7b187f52015-08-24 14:07:08 -070078 * Examples:
79 *
Alexander Afanasyev8828ca62015-07-02 13:40:09 -070080 * toHex("Hello, World!") == "48656C6C6F2C20776F726C6421"
81 * toHex("Hello, World!", false) == "48656c6c6f2c20776f726c6421"
82 *
83 * Each octet is always represented as two hex characters ("00" for octet==0).
84 *
85 * The output string is a continuous sequence of hex characters without any whitespace separators.
Alexander Afanasyev809805d2014-02-17 17:20:33 -080086 */
Alexander Afanasyev8828ca62015-07-02 13:40:09 -070087std::string
Davide Pesaventofdda2412017-01-28 18:53:51 -050088toHex(const uint8_t* buffer, size_t length, bool wantUpperCase = true);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070089
Alexander Afanasyev8828ca62015-07-02 13:40:09 -070090/**
91 * @brief Return the hex representation of the bytes in the @p buffer to the output stream @p os
92 *
93 * @param buffer The array of bytes
Davide Pesaventofdda2412017-01-28 18:53:51 -050094 * @param wantUpperCase if true (default) output use uppercase for hex values
Alexander Afanasyev8828ca62015-07-02 13:40:09 -070095 */
96std::string
Davide Pesaventofdda2412017-01-28 18:53:51 -050097toHex(const Buffer& buffer, bool wantUpperCase = true);
Alexander Afanasyev809805d2014-02-17 17:20:33 -080098
Alexander Afanasyev8828ca62015-07-02 13:40:09 -070099/**
Davide Pesavento03115ce2017-01-27 01:17:55 -0500100 * @brief Convert the hex character to an integer from 0 to 15, or -1 if not a hex character
101 */
102int
103fromHexChar(char c);
104
105/**
Alexander Afanasyev8828ca62015-07-02 13:40:09 -0700106 * @brief Convert the hex string to buffer
107 * @param hexString sequence of pairs of hex numbers (lower and upper case can be mixed)
108 * without any whitespace separators (e.g., "48656C6C6F2C20776F726C6421")
109 * @throw StringHelperError if input is invalid
110 */
Davide Pesavento03115ce2017-01-27 01:17:55 -0500111shared_ptr<Buffer>
Alexander Afanasyev8828ca62015-07-02 13:40:09 -0700112fromHex(const std::string& hexString);
Alexander Afanasyev809805d2014-02-17 17:20:33 -0800113
114/**
Alexander Afanasyev8828ca62015-07-02 13:40:09 -0700115 * @brief Decode a percent-encoded string
116 * @see RFC 3986 section 2
117 *
118 * When % is not followed by two hex characters, the output is not transformed.
119 *
philo7b187f52015-08-24 14:07:08 -0700120 * Examples:
121 *
122 * unescape("hello%20world") == "hello world"
123 * unescape("hello%20world%FooBar") == "hello world%FooBar"
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800124 */
Alexander Afanasyev8828ca62015-07-02 13:40:09 -0700125std::string
126unescape(const std::string& str);
Alexander Afanasyevaf283d82014-01-03 13:23:34 -0800127
128} // namespace ndn
129
Davide Pesavento03115ce2017-01-27 01:17:55 -0500130#endif // NDN_UTIL_STRING_HELPER_HPP