-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjbconversion.hpp
More file actions
58 lines (51 loc) · 1.55 KB
/
jbconversion.hpp
File metadata and controls
58 lines (51 loc) · 1.55 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
/////////////////////////////////////////////////////////////////////////////
// Name: jbconversion.hpp
// Purpose: String conversion and manipulation utilities
// Author: Jan Buchholz
// Created: 2025-11-12
/////////////////////////////////////////////////////////////////////////////
#pragma once
#include <wx/filename.h>
#include <string>
inline wxString toWxString(const std::string& str) {
return wxString::FromUTF8(str.c_str());
}
inline std::string toStdString(const wxString& str) {
return {str.mb_str(wxConvUTF8)};
}
inline std::string getPathSeparator() {
const wxString sep = wxFileName::GetPathSeparator(wxPATH_NATIVE);
return toStdString(sep);
}
inline std::string escapeSpecials(const std::string& input) {
std::string result = input;
size_t pos = 0;
// \n -> \\n
while ((pos = result.find('\n', pos)) != std::string::npos) {
result.replace(pos, 1, "\\n");
pos += 2;
}
pos = 0;
// \t -> \\t
while ((pos = result.find('\t', pos)) != std::string::npos) {
result.replace(pos, 1, "\\t");
pos += 2;
}
return result;
}
inline std::string unescapeSpecials(const std::string& input) {
std::string result = input;
size_t pos = 0;
// \\n → \n
while ((pos = result.find("\\n", pos)) != std::string::npos) {
result.replace(pos, 2, "\n");
pos += 1;
}
pos = 0;
// \\t → \t
while ((pos = result.find("\\t", pos)) != std::string::npos) {
result.replace(pos, 2, "\t");
pos += 1;
}
return result;
}