-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add CharsetAutoSelector (#222) #245
Closed
Closed
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include "CharsetAutoSelector.h" | ||
#include "ASCIICharset.h" | ||
#include "DefaultCharset.h" | ||
#include "UTF8Charset.h" | ||
#include "UTF16Charset.h" | ||
#include "UTF32Charset.h" | ||
#include <sstream> | ||
#include <iomanip> | ||
#include <iostream> | ||
#include <algorithm> | ||
|
||
using namespace Euphony; | ||
|
||
HexVector CharsetAutoSelector::select(std::string src) { | ||
|
||
HexVector asciiResult = ASCIICharset().encode(src); | ||
HexVector defaultResult = DefaultCharset().encode(src); | ||
HexVector utf8Result = UTF8Charset().encode(src); | ||
HexVector utf16Result = UTF16Charset().encode(src); | ||
HexVector utf32Result = UTF32Charset().encode(src); | ||
|
||
HexVector results[] = {asciiResult, defaultResult, utf8Result, utf16Result, utf32Result}; | ||
|
||
std::sort(results, results+5, [](HexVector& left, HexVector& right) { | ||
return left.getSize() < right.getSize(); | ||
}); | ||
|
||
return results[0]; | ||
} | ||
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,16 @@ | ||
#ifndef EUPHONY_CHARSETAUTOSELECTOR_H | ||
#define EUPHONY_CHARSETAUTOSELECTOR_H | ||
|
||
#include "Charset.h" | ||
|
||
namespace Euphony { | ||
|
||
class CharsetAutoSelector : public Charset { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder why you implemented |
||
public: | ||
CharsetAutoSelector() = default; | ||
~CharsetAutoSelector() = default; | ||
HexVector select(std::string src); | ||
}; | ||
} | ||
|
||
#endif //EUPHONY_CHARSETAUTOSELECTOR_H |
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
81 changes: 81 additions & 0 deletions
81
euphony/src/main/cpp/tests/charset/charsetAutoSelectorTest.cpp
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,81 @@ | ||
#include <gtest/gtest.h> | ||
#include <Definitions.h> | ||
#include <tuple> | ||
#include <charset/ASCIICharset.h> | ||
#include <charset/DefaultCharset.h> | ||
#include <charset/UTF8Charset.h> | ||
#include <charset/UTF16Charset.h> | ||
#include <charset/UTF32Charset.h> | ||
#include <iostream> | ||
#include <algorithm> | ||
|
||
using namespace Euphony; | ||
|
||
typedef std::tuple<std::string, std::string> TestParamType; | ||
|
||
class CharsetAutoSelectorTestFixture : public ::testing::TestWithParam<TestParamType> { | ||
|
||
public: | ||
void openCharset() { | ||
EXPECT_EQ(asciiCharset, nullptr); | ||
asciiCharset = new ASCIICharset(); | ||
ASSERT_NE(asciiCharset, nullptr); | ||
|
||
EXPECT_EQ(defaultCharset, nullptr); | ||
defaultCharset = new DefaultCharset(); | ||
ASSERT_NE(defaultCharset, nullptr); | ||
|
||
EXPECT_EQ(utf8Charset, nullptr); | ||
utf8Charset = new UTF8Charset(); | ||
ASSERT_NE(utf8Charset, nullptr); | ||
|
||
EXPECT_EQ(utf16Charset, nullptr); | ||
utf16Charset = new UTF16Charset(); | ||
ASSERT_NE(utf16Charset, nullptr); | ||
|
||
EXPECT_EQ(utf32Charset, nullptr); | ||
utf32Charset = new UTF16Charset(); | ||
ASSERT_NE(utf32Charset, nullptr); | ||
} | ||
|
||
Charset *asciiCharset = nullptr; | ||
Charset *defaultCharset = nullptr; | ||
Charset *utf8Charset = nullptr; | ||
Charset *utf16Charset = nullptr; | ||
Charset *utf32Charset = nullptr; | ||
}; | ||
|
||
TEST_P(CharsetAutoSelectorTestFixture, SelectTest) { | ||
openCharset(); | ||
|
||
std::string source; | ||
std::string expectedResult; | ||
|
||
std::tie(source, expectedResult) = GetParam(); | ||
|
||
HexVector asciiResult = asciiCharset->encode(source); | ||
HexVector defaultResult = defaultCharset->encode(source); | ||
HexVector utf8Result = utf8Charset->encode(source); | ||
HexVector utf16Result = utf16Charset->encode(source); | ||
HexVector utf32Result = utf32Charset->encode(source); | ||
|
||
HexVector results[] = {asciiResult, defaultResult, utf8Result, utf16Result, utf32Result}; | ||
|
||
std::sort(results, results+5, [](HexVector& left, HexVector& right) { | ||
return left.getSize() < right.getSize(); | ||
}); | ||
|
||
HexVector actualResult = results[0]; | ||
|
||
EXPECT_EQ(actualResult.toString(), expectedResult); | ||
} | ||
|
||
INSTANTIATE_TEST_CASE_P( | ||
CharsetAutoSelectorTestSuite, | ||
CharsetAutoSelectorTestFixture, | ||
::testing::Values( | ||
TestParamType("a", "61"), | ||
TestParamType("b", "62"), | ||
TestParamType("가", "eab080"), | ||
TestParamType("나", "eb8298") | ||
)); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good job to make
select
functionIt'll make a shortest encoded data from
sort
.Seeing this result, I thought it would be good to implement the Charset interface called AutoCharset.
However, what I expected is that
select
is gonna tell us type of charset.this is utility for charset selection. so, function definition might be like below.
your proposal is also this one #222
Chraset* ChrasetAutoSelector::select(std::string src);
or
shared_ptr<Charset> CharsetAutoSelector::select(std::string src);
what do you think about it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think so. It has been revised and I will commit soon.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@DahyeonWoo
Here is my thinking.
There are so many methods to implement
CharsetAutoSelector
:)Firstly, analyze the charset's charecteristics.
My pseudo code :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And.. if return type is
CharsetType
,we can use like below.
CharsetType
is defined in Definition.heuphony/euphony/src/main/cpp/core/Definitions.h
Line 62 in 04fdaac
Which one do you like?
if you have some question, let me know :)