From f58f8a9fc33516d327620fe0059b8b855a246d30 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Thu, 24 Jan 2019 23:13:20 +0100 Subject: Move message composer to dchat core --- depends/dchat_core | 2 +- include/MessageComposer.hpp | 32 --------- src/MessageComposer.cpp | 159 -------------------------------------------- tests/main.cpp | 114 +------------------------------ 4 files changed, 3 insertions(+), 304 deletions(-) delete mode 100644 include/MessageComposer.hpp delete mode 100644 src/MessageComposer.cpp diff --git a/depends/dchat_core b/depends/dchat_core index c070f5f..06f6f7f 160000 --- a/depends/dchat_core +++ b/depends/dchat_core @@ -1 +1 @@ -Subproject commit c070f5f9edc0acb0abe853036dcd9157ca24fc1b +Subproject commit 06f6f7f2f2bee4a967a9c4d46f9ea97a8a5b1d44 diff --git a/include/MessageComposer.hpp b/include/MessageComposer.hpp deleted file mode 100644 index 5af065d..0000000 --- a/include/MessageComposer.hpp +++ /dev/null @@ -1,32 +0,0 @@ -#pragma once - -#include -#include - -namespace dchat -{ - struct Range - { - int start; - int end; - - int length() const - { - return end - start; - } - }; - - struct MessagePart - { - enum class Type - { - TEXT, - EMOJI - }; - - Type type; - Range textRange; - }; - - void compose(const Glib::ustring &text, std::function callbackFunc); -} \ No newline at end of file diff --git a/src/MessageComposer.cpp b/src/MessageComposer.cpp deleted file mode 100644 index 3ff0d76..0000000 --- a/src/MessageComposer.cpp +++ /dev/null @@ -1,159 +0,0 @@ -#include "../include/MessageComposer.hpp" -#include -#include - -namespace dchat -{ - enum class Token - { - NONE, - END_OF_FILE, - - TEXT, - TYPE - }; - - struct Tokenizer - { - Tokenizer(const Glib::ustring *_text) - { - assert(_text); - text = _text; - index = 0; - length = text->size(); - - identifierRange = { 0, 0 }; - } - - enum class EnclosedType - { - TEXT, - DATA - }; - - EnclosedType parseEnclosedData(char endSymbol) - { - bool foundEndOfType = false; - - while(index < length) - { - char c = getChar(); - ++index; - if(c == endSymbol) - { - foundEndOfType = true; - break; - } - } - - if(!foundEndOfType) - return EnclosedType::TEXT; - - return EnclosedType::DATA; - } - - Token next() - { - if(index >= length) - return Token::END_OF_FILE; - - char c = getChar(); - if(c == '[') - { - usize start = index; - ++index; - if(parseEnclosedData(']') == EnclosedType::TEXT) - { - identifierRange.start = start; - identifierRange.end = index; - return Token::TEXT; - } - - if(index == length || getChar() != '(') - { - identifierRange.start = start; - identifierRange.end = index; - return Token::TEXT; - } - - typeRange.start = start + 1; - typeRange.end = index - 1; - typeDataRange.start = index + 1; - - ++index; - switch(parseEnclosedData(')')) - { - case EnclosedType::TEXT: - { - identifierRange.start = start; - identifierRange.end = index; - return Token::TEXT; - } - case EnclosedType::DATA: - { - typeDataRange.end = index - 1; - return Token::TYPE; - } - } - } - else - { - identifierRange.start = index; - ++index; - while(index < length) - { - c = getChar(); - if(c == '[') - break; - ++index; - } - identifierRange.end = index; - return Token::TEXT; - } - - assert(false); - return Token::NONE; - } - - char getChar() const - { - assert(index < length); - return (*text)[index]; - } - - const Glib::ustring *text; - usize index; - usize length; - - Range identifierRange; - Range typeRange; - Range typeDataRange; - }; - - void compose(const Glib::ustring &text, std::function callbackFunc) - { - Tokenizer tokenizer(&text); - Token token = tokenizer.next(); - while(token != Token::END_OF_FILE) - { - if(token == Token::TEXT) - { - callbackFunc(MessagePart { MessagePart::Type::TEXT, tokenizer.identifierRange }); - token = tokenizer.next(); - } - else if(token == Token::TYPE) - { - if(text.compare(tokenizer.typeRange.start, tokenizer.typeRange.length(), "emoji") != 0) - { - Range typeToTextRange = { tokenizer.typeRange.start - 1, tokenizer.typeDataRange.end + 1 }; - callbackFunc(MessagePart{ MessagePart::Type::TEXT, typeToTextRange }); - } - else - { - callbackFunc(MessagePart { MessagePart::Type::EMOJI, tokenizer.typeDataRange }); - } - token = tokenizer.next(); - } - } - } -} \ No newline at end of file diff --git a/tests/main.cpp b/tests/main.cpp index b18626c..1b9231c 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -1,5 +1,5 @@ -#include "../include/MessageComposer.hpp" #include "../include/DynamicImage.hpp" +#include #include #include #include @@ -26,7 +26,7 @@ static void applyRichText(Gtk::TextView *textView, const Glib::ustring &text) buffer->set_text(""); Gtk::TextIter iter = buffer->get_iter_at_offset(0); - dchat::compose(text, [textView, &text, &iter, &buffer](dchat::MessagePart messagePart) + dchat::compose(text.data(), text.bytes(), [textView, &text, &iter, &buffer](dchat::MessagePart messagePart) { switch(messagePart.type) { @@ -76,115 +76,5 @@ static int testVisual(int argc, char **argv) int main(int argc, char **argv) { - std::vector expect = - { - dchat::MessagePart { dchat::MessagePart::Type::TEXT, dchat::Range{ 0, 4 } }, - dchat::MessagePart { dchat::MessagePart::Type::EMOJI, dchat::Range{ 12, 16 } }, - dchat::MessagePart { dchat::MessagePart::Type::TEXT, dchat::Range{ 17, 21 } } - }; - - std::vector expectText = - { - "abc ", - "cool", - " def" - }; - - int index = 0; - const char *str = "abc [emoji](cool) def"; - - auto compareFunc = [&str, &index, &expect, &expectText](dchat::MessagePart messagePart) - { - REQUIRE(index < expect.size()); - REQUIRE_EQUAL((int)messagePart.type, (int)expect[index].type); - REQUIRE_EQUAL(messagePart.textRange.start, expect[index].textRange.start); - REQUIRE_EQUAL(messagePart.textRange.end, expect[index].textRange.end); - int length = messagePart.textRange.end - messagePart.textRange.start; - if(strncmp(str + messagePart.textRange.start, expectText[index], length) != 0) - { - fprintf(stderr, "Assert failed: |%.*s| == |%.*s|\n", length, str + messagePart.textRange.start, length, expectText[index]); - exit(EXIT_FAILURE); - } - ++index; - }; - - dchat::compose(str, compareFunc); - - { - expect = - { - dchat::MessagePart { dchat::MessagePart::Type::TEXT, dchat::Range{ 0, 4 } }, - dchat::MessagePart { dchat::MessagePart::Type::TEXT, dchat::Range{ 4, 20 } } - }; - - expectText = - { - "abc ", - "[emoji](cool def" - }; - - index = 0; - str = "abc [emoji](cool def"; - dchat::compose(str, compareFunc); - } - - { - expect = - { - dchat::MessagePart { dchat::MessagePart::Type::TEXT, dchat::Range{ 0, 4 } }, - dchat::MessagePart { dchat::MessagePart::Type::TEXT, dchat::Range{ 4, 11 } }, - dchat::MessagePart { dchat::MessagePart::Type::TEXT, dchat::Range{ 11, 22 } } - }; - - expectText = - { - "abc ", - "[emoji]", - " (cool def)" - }; - - index = 0; - str = "abc [emoji] (cool def)"; - dchat::compose(str, compareFunc); - } - - { - expect = - { - dchat::MessagePart { dchat::MessagePart::Type::TEXT, dchat::Range{ 0, 4 } }, - dchat::MessagePart { dchat::MessagePart::Type::TEXT, dchat::Range{ 4, 20 } }, - dchat::MessagePart { dchat::MessagePart::Type::TEXT, dchat::Range{ 20, 24 } } - }; - - expectText = - { - "abc ", - "[notemoji](cool)", - " def" - }; - - index = 0; - str = "abc [notemoji](cool) def"; - dchat::compose(str, compareFunc); - } - - { - expect = - { - dchat::MessagePart { dchat::MessagePart::Type::TEXT, dchat::Range{ 0, 12 } }, - dchat::MessagePart { dchat::MessagePart::Type::EMOJI, dchat::Range{ 20, 71 } } - }; - - expectText = - { - "Hello world ", - "https://discordemoji.com/assets/emoji/PeepoHide.png" - }; - - index = 0; - str = "Hello world [emoji](https://discordemoji.com/assets/emoji/PeepoHide.png)"; - dchat::compose(str, compareFunc); - } - return testVisual(argc, argv); } -- cgit v1.2.3