forked from josemariasola/StringNAndBlockStream
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringN.h
More file actions
58 lines (46 loc) · 1.29 KB
/
StringN.h
File metadata and controls
58 lines (46 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/* StringPacker.h
2015-11-08 - 2018-06-30
Esp. Ing. José María Sola
Professor
UTN FRBA */
#ifndef HEADER_STRINGN_INCLUDED
#define HEADER_STRINGN_INCLUDED
#include <string>
#include <array>
// Type: String<N>
template<std::size_t N> using String = std::array<char,N>;
/* Builds a string from a String<N> upto its first null characater or end,
e.g.: s=UnpackString(a) */
template<std::size_t N>
inline std::string UnpackString(const String<N>& a){
std::string s{""};
for(auto c : a){
if(c=='\0')
break;
s.push_back(c);
}
return s;
}
/* Provides the constructs to pack strings with a simple and clear syntax:
a=PackString(s)
Implemantation based upon
http://stackoverflow.com/questions/8165659/why-cant-c-deduce-template-type-from-assignment
*/
struct PackString{
std::string theString;
// Build from string
PackString(const std::string& aString) : theString(aString) {}
// User-defined conversion: from string to String<N>
template<std::size_t N>
inline operator String<N>(){
String<N> aString; // undefined content
auto len = theString.length();
if( len < N ){
theString.copy(aString.data(), len);
aString.at(len) = '\0'; // form aString.at(len+1) to aString.at(N-1) undefined content.
}else
theString.copy(aString.data(), N);
return aString;
}
};
#endif