Skip to content

Commit c7fd1cd

Browse files
committed
writer-json-common: common utils for specific JSON writers
This commit moves sanitizeUTF8() from writer-json.cc to writer-json-common.cc. Related: #115
1 parent 8626e01 commit c7fd1cd

File tree

4 files changed

+61
-10
lines changed

4 files changed

+61
-10
lines changed

src/lib/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,5 @@ add_library(cs STATIC
4444
writer-cov.cc
4545
writer-html.cc
4646
writer-json.cc
47+
writer-json-common.cc
4748
)

src/lib/writer-json-common.cc

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (C) 2011 - 2023 Red Hat, Inc.
3+
*
4+
* This file is part of csdiff.
5+
*
6+
* csdiff is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* any later version.
10+
*
11+
* csdiff is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with csdiff. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
#include "writer-json-common.hh"
21+
22+
#include <boost/nowide/utf/convert.hpp>
23+
24+
std::string sanitizeUTF8(const std::string &str)
25+
{
26+
using boost::nowide::utf::convert_string;
27+
28+
// every non-UTF8 sequence will be replaced with 0xEF 0xBF 0xBD which
29+
// corresponds to REPLACEMENT CHARACTER U+FFFD
30+
return convert_string<char>(str.data(), str.data() + str.size());
31+
}

src/lib/writer-json-common.hh

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (C) 2011 - 2023 Red Hat, Inc.
3+
*
4+
* This file is part of csdiff.
5+
*
6+
* csdiff is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* any later version.
10+
*
11+
* csdiff is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with csdiff. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
#ifndef H_GUARD_WRITER_JSON_COMMON_H
21+
#define H_GUARD_WRITER_JSON_COMMON_H
22+
23+
#include <string>
24+
25+
/// sanitize byte sequences that are not valid in UTF-8 encoding
26+
std::string sanitizeUTF8(const std::string &str);
27+
28+
#endif /* H_GUARD_WRITER_JSON_COMMON_H */

src/lib/writer-json.cc

+1-10
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,16 @@
2222
#include "abstract-tree.hh"
2323
#include "regex.hh"
2424
#include "version.hh"
25+
#include "writer-json-common.hh"
2526

2627
#include <algorithm>
2728
#include <queue>
2829

2930
#include <boost/json/src.hpp>
3031
#include <boost/lexical_cast.hpp>
31-
#include <boost/nowide/utf/convert.hpp>
3232

3333
using namespace boost::json;
3434

35-
static inline std::string sanitizeUTF8(const std::string &str)
36-
{
37-
using boost::nowide::utf::convert_string;
38-
39-
// every non-UTF8 sequence will be replaced with 0xEF 0xBF 0xBD which
40-
// corresponds to REPLACEMENT CHARACTER U+FFFD
41-
return convert_string<char>(str.data(), str.data() + str.size());
42-
}
43-
4435
static void prettyPrint(std::ostream&, const value&, std::string* = nullptr);
4536

4637
static inline void prettyPrintArray(

0 commit comments

Comments
 (0)