Skip to content
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

CCodeBaseの変換テストを追加する #1614

Merged
merged 1 commit into from
Mar 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 46 additions & 2 deletions sakura_core/charset/CCodeBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
#define SAKURA_CCODEBASE_1AB194FB_933C_495E_A3A3_62E117C72644_H_
#pragma once

#include <cstddef>
#include <string>
#include <string_view>

#include "mem/CNativeW.h"
#include "CEol.h"

Expand All @@ -39,6 +43,12 @@ enum EConvertResult{

struct CommonSetting_Statusbar;

//! 変換元バイナリシーケンスを表す型。
using BinarySequenceView = std::basic_string_view<std::byte>;

//! 復元後バイナリシーケンスを表す型。
using BinarySequence = std::basic_string<std::byte>;

/*!
文字コード基底クラス。

Expand All @@ -47,8 +57,42 @@ struct CommonSetting_Statusbar;
*/
class CCodeBase{
public:
virtual ~CCodeBase(){}
// virtual bool IsCode(const CMemory* pMem){return false;} //!< 特定コードであればtrue
virtual ~CCodeBase() noexcept = default;

/*!
特定コードをUnicodeにエンコードする

@param [in] cSrc 変換対象のバイナリシーケンス
@param [out,opt] pResult 変換結果を受け取る変数
@returns サクラエディタ仕様のUnicode文字列
*/
virtual CNativeW CodeToUnicode( BinarySequenceView cSrc, bool* pResult = nullptr )
{
CMemory cmemSrc( cSrc.data(), cSrc.size() );
CNativeW cDest;
auto result = CodeToUnicode( cmemSrc, &cDest );
if( pResult ){
*pResult = result == RESULT_COMPLETE;
}
return cDest;
}

/*!
Unicodeを特定コードにデコードする

@param [in] cSrc 変換対象のUnicodeシーケンス
@param [out,opt] pResult 変換結果を受け取る変数
@returns バイナリシーケンス
*/
virtual BinarySequence UnicodeToCode( const CNativeW& cSrc, bool* pResult = nullptr )
{
CMemory cDest;
auto result = UnicodeToCode( cSrc, &cDest );
if( pResult ){
*pResult = result == RESULT_COMPLETE;
}
return BinarySequence( static_cast<std::byte*>(cDest.GetRawPtr()), cDest.GetRawLength() );
}

//文字コード変換
virtual EConvertResult CodeToUnicode(const CMemory& cSrc, CNativeW* pDst)=0; //!< 特定コード → UNICODE 変換
Expand Down
12 changes: 11 additions & 1 deletion sakura_core/charset/CCodeFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
#define SAKURA_CCODEFACTORY_A5C6C204_F9BD_42BA_A5CD_1B086833CCA4_H_
#pragma once

class CCodeBase;
#include <memory>
#include "charset/CCodeBase.h"

class CCodeFactory{
public:
Expand All @@ -36,5 +37,14 @@ class CCodeFactory{
ECodeType eCodeType, //!< 文字コード
int nFlag //!< bit 0: MIME Encodeされたヘッダをdecodeするかどうか
);

//! eCodeTypeに適合する CCodeBaseインスタンス を生成
static std::unique_ptr<CCodeBase> CreateCodeBase(
ECodeType eCodeType //!< 文字コード
)
{
return std::unique_ptr<CCodeBase>( CreateCodeBase( eCodeType, 0 ) );
}
};

#endif /* SAKURA_CCODEFACTORY_A5C6C204_F9BD_42BA_A5CD_1B086833CCA4_H_ */