-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove regex lrc lib, use my own, regex sucks
- Loading branch information
Dr.Abc
committed
Sep 1, 2024
1 parent
1cd1748
commit f596136
Showing
11 changed files
with
186 additions
and
405 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#pragma once | ||
#include <string> | ||
#include <vector> | ||
|
||
namespace lrc { | ||
using lrcattribute_t = struct lrcattribute_s | ||
{ | ||
std::u8string Key = u8""; | ||
std::u8string Value = u8""; | ||
}; | ||
class CLrcLine | ||
{ | ||
public: | ||
std::u8string CurrentLyric = u8""; | ||
time_t StartTime = 0; | ||
time_t EndTime = 0; | ||
const char* GetLyric(); | ||
}; | ||
class CLrcCollection { | ||
public: | ||
std::vector<CLrcLine*> Lines = {}; | ||
std::vector<lrcattribute_t*> Attributes = {}; | ||
size_t Size(); | ||
CLrcLine* LyricAt(time_t time); | ||
~CLrcCollection(); | ||
}; | ||
extern CLrcCollection* LrcParser(const char8_t* lrc); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
#include "liblrc.h" | ||
|
||
#include <algorithm> | ||
#include <iomanip> | ||
#include <sstream> | ||
|
||
namespace lrc{ | ||
CLrcCollection* LrcParser(const char8_t* lrc) { | ||
std::vector<std::u8string> splited; | ||
std::u8string s = lrc; | ||
std::u8string token; | ||
std::basic_istringstream<char8_t> tokenStream(s); | ||
while (std::getline(tokenStream, token, u8'\n')) { | ||
splited.push_back(token); | ||
} | ||
|
||
CLrcCollection* collection = new CLrcCollection(); | ||
for (auto iter = splited.begin(); iter != splited.end(); iter++) { | ||
std::u8string line = *iter; | ||
if (*line.begin() != u8'[') | ||
continue; | ||
size_t tagend = line.rfind(u8']'); | ||
//wtf??? | ||
if (tagend == std::u8string::npos) | ||
continue; | ||
//Is Attribute | ||
if (tagend == line.size() - 1) { | ||
bool invalue = false; | ||
lrcattribute_t* attribute = new lrcattribute_t(); | ||
for (size_t i = 1; i < line.size() - 1; i++) { | ||
char8_t c = line[i]; | ||
if (c == u8':') { | ||
invalue = true; | ||
continue; | ||
} | ||
if (invalue) | ||
attribute->Value += c; | ||
else | ||
attribute->Key += c; | ||
} | ||
collection->Attributes.push_back(attribute); | ||
} | ||
//Is Lyric | ||
else { | ||
std::u8string header = line.substr(0, tagend + 1); | ||
std::u8string lrc = line.substr(tagend + 1); | ||
|
||
std::u8string timestamp = u8""; | ||
CLrcLine* lineitem = nullptr; | ||
for (size_t i = 0; i < header.size(); i++) { | ||
char8_t c = header[i]; | ||
switch (c) | ||
{ | ||
case u8'[': { | ||
lineitem = new CLrcLine(); | ||
break; | ||
} | ||
case u8']': { | ||
if (lineitem) { | ||
time_t m, s, ms; | ||
char dem; | ||
std::istringstream ss(reinterpret_cast<const char*>(timestamp.c_str())); | ||
ss >> m >> dem >> s >> dem >> ms; | ||
lineitem->StartTime = m * 60000 + s * 1000 + ms; | ||
lineitem->CurrentLyric = lrc; | ||
collection->Lines.push_back(lineitem); | ||
lineitem = nullptr; | ||
} | ||
break; | ||
} | ||
default: | ||
timestamp += c; | ||
break; | ||
} | ||
} | ||
//wtf line | ||
if (lineitem) | ||
delete lineitem; | ||
} | ||
} | ||
std::sort(collection->Lines.begin(), collection->Lines.end(), [](const CLrcLine* a, const CLrcLine* b) { | ||
return a->StartTime < b->StartTime; | ||
}); | ||
if (collection->Lines.size() > 1) { | ||
for (size_t i = 0; i < collection->Lines.size() - 1; ++i) { | ||
collection->Lines[i]->EndTime = collection->Lines[i + 1]->StartTime; | ||
} | ||
} | ||
return collection; | ||
} | ||
|
||
const char* CLrcLine::GetLyric(){ | ||
return reinterpret_cast<const char*>(CurrentLyric.c_str()); | ||
} | ||
|
||
size_t CLrcCollection::Size(){ | ||
return Lines.size(); | ||
} | ||
|
||
CLrcLine* CLrcCollection::LyricAt(time_t time){ | ||
for (auto iter = Lines.rbegin(); iter != Lines.rend(); iter++) { | ||
auto line = (*iter); | ||
if (line->StartTime <= time) | ||
return line; | ||
} | ||
return nullptr; | ||
} | ||
|
||
CLrcCollection::~CLrcCollection() { | ||
for (auto iter = Lines.begin(); iter != Lines.end(); iter++) { | ||
delete (*iter); | ||
} | ||
for (auto iter = Attributes.begin(); iter != Attributes.end(); iter++) { | ||
delete (*iter); | ||
} | ||
Lines.clear(); | ||
Attributes.clear(); | ||
} | ||
} |
Oops, something went wrong.