This repository has been archived by the owner on Oct 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
AsciiPunctuation.cpp
68 lines (60 loc) · 1.86 KB
/
AsciiPunctuation.cpp
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
#include "foo_musicbrainz.h"
#include "MetadataProcessor.h"
namespace foo_musicbrainz {
class AsciiPunctuation : public MetadataProcessor {
unsigned int get_entities() { return track_entity | release_entity; }
bool is_enabled() { return Preferences::ascii_punctuation; }
void replace(pfc::string8 &str) {
pfc::string tmp(str);
for (size_t i = 0; i < replacement_size; i++) {
auto what = replacements[i][0];
auto with = replacements[i][1];
tmp = tmp.replace(what, with);
}
str = tmp.get_ptr();
}
void process(Release &release) {
auto tmp = release.get_title();
replace(tmp);
release.set_title(tmp);
}
void process(Track &track) {
auto tmp = track.get_title();
replace(tmp);
track.set_title(tmp);
}
static const char *replacements[][2];
static const int replacement_size;
};
// Only to be used with string literals — causes memory leaks.
# define u(str) utf8(L##str)
const char *utf8(const wchar_t *str) {
pfc::stringcvt::string_utf8_from_wide converted(str);
auto tmp = new char[converted.length() + 1];
strcpy(tmp, converted.get_ptr());
return tmp;
}
const char *AsciiPunctuation::replacements[][2] = {
{ u("…"), u("...") },
{ u("‘"), u("'") },
{ u("’"), u("'") },
{ u("‚"), u("'") },
{ u("“"), u("\"") },
{ u("”"), u("\"") },
{ u("„"), u("\"") },
{ u("′"), u("'") },
{ u("″"), u("\"") },
{ u("‹"), u("<") },
{ u("›"), u(">") },
{ u("«"), u("\"") },
{ u("»"), u("\"") },
{ u("‐"), u("-") },
{ u("‒"), u("-") },
{ u("–"), u("-") },
{ u("−"), u("-") },
{ u("—"), u("-") },
{ u("―"), u("-") }
};
const int AsciiPunctuation::replacement_size = sizeof(AsciiPunctuation::replacements) / sizeof(const char *) / 2;
service_factory_single_t<AsciiPunctuation> metadata_processor;
}