aboutsummaryrefslogtreecommitdiff
path: root/zencore/streamutil.cpp
blob: d3ed5ceaa543ced0584fd1f1554fe95a176e9e5f (plain) (blame)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright Epic Games, Inc. All Rights Reserved.

#include <zencore/streamutil.h>
#include <zencore/string.h>

namespace zen {

BinaryWriter&
operator<<(BinaryWriter& writer, const std::string_view& value)
{
	writer.Write(value.data(), value.size());
	writer << uint8_t(0);

	return writer;
}

BinaryReader&
operator>>(BinaryReader& reader, std::string& value)
{
	for (;;)
	{
		uint8_t x;
		reader.Read(&x, 1);

		if (x == 0)
			return reader;

		value.push_back(char(x));
	}
}

BinaryWriter&
operator<<(BinaryWriter& writer, const std::wstring_view& value)
{
	// write as utf8

	ExtendableStringBuilder<128> utf8;
	WideToUtf8(value, utf8);

	writer.Write(utf8.c_str(), utf8.Size() + 1);

	return writer;
}

BinaryReader&
operator>>(BinaryReader& reader, std::wstring& value)
{
	// read as utf8

	std::string v8;
	reader >> v8;

	ExtendableWideStringBuilder<128> wstr;
	Utf8ToWide(v8, wstr);

	value = wstr.c_str();

	return reader;
}

TextWriter&
operator<<(TextWriter& writer, const zen::SHA1& value)
{
	zen::SHA1::String_t buffer;
	value.ToHexString(buffer);

	writer.Write(buffer, zen::SHA1::StringLength);

	return writer;
}

TextWriter&
operator<<(TextWriter& writer, const zen::BLAKE3& value)
{
	zen::BLAKE3::String_t buffer;
	value.ToHexString(buffer);

	writer.Write(buffer, zen::BLAKE3::StringLength);

	return writer;
}

TextWriter&
operator<<(TextWriter& writer, const zen::IoHash& value)
{
	zen::IoHash::String_t buffer;
	value.ToHexString(buffer);

	writer.Write(buffer, zen::IoHash::StringLength);

	return writer;
}

TextWriter&
operator<<(TextWriter& writer, const std::wstring_view& value)
{
	ExtendableStringBuilder<128> v8;
	WideToUtf8(value, v8);

	writer.Write(v8.c_str(), v8.Size());
	return writer;
}

}  // namespace zen