sources/list.cpp

changeset 163
5948441a1951
child 180
2e7225dbd9b2
equal deleted inserted replaced
162:c9c0f1b62e42 163:5948441a1951
1 /*
2 Copyright 2016 Teemu Piippo
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8
9 1. Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14 3. Neither the name of the copyright holder nor the names of its
15 contributors may be used to endorse or promote products derived from
16 this software without specific prior written permission.
17
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
21 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
22 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "list.h"
32 #include "mystring.h"
33
34 BEGIN_ZFC_NAMESPACE
35
36 /*!
37 * \brief Finds a suitable string representation for the provided byte.
38 * \param byte Byte to represent.
39 * \returns the string representation for the byte.
40 */
41 const char* representByte(unsigned char byte)
42 {
43 static char buffer[8];
44
45 if (byte < 8)
46 {
47 buffer[0] = '\\';
48 buffer[1] = byte + '0';
49 buffer[2] = '\0';
50 return buffer;
51 }
52 else switch(byte)
53 {
54 case '\a': return "\\a";
55 case '\b': return "\\b";
56 case '\t': return "\\t";
57 case '\n': return "\\n";
58 case '\v': return "\\v";
59 case '\f': return "\\f";
60 case '\r': return "\\r";
61 case '\e': return "\\e";
62 case '"': return "\\\"";
63 case '\\': return "\\\\";
64 default:
65 if (isprint(byte))
66 {
67 buffer[0] = byte;
68 buffer[1] = '\0';
69 return buffer;
70 }
71 else
72 {
73 snprintf(buffer, sizeof buffer, "\\x%2x", byte);
74 return buffer;
75 }
76 }
77 }
78
79 /*!
80 * \brief Constructs a byte array from an initializer list containing bytes.
81 * \param initializerList List of bytes.
82 */
83 ByteArray::ByteArray(std::initializer_list<unsigned char> initializerList) :
84 Vector<unsigned char>(initializerList) {}
85
86 /*!
87 * \returns a quoted representation of the contents of the byte array.
88 */
89 String ByteArray::quote() const
90 {
91 String result;
92
93 for (unsigned char byte : *this)
94 result += representByte(byte);
95
96 return "\"" + result + "\"";
97 }
98
99 END_ZFC_NAMESPACE

mercurial