Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions sakura_core/CCodeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@
static bool _CheckSavingEolcode(const CDocLineMgr& pcDocLineMgr, CEol cEolType)
{
bool bMix = false;
if( cEolType == EOL_NONE ){ //改行コード変換なし
if( cEolType.IsNone() ){ //改行コード変換なし
CEol cEolCheck; //比較対象EOL
const CDocLine* pcDocLine = pcDocLineMgr.GetDocLineTop();
if( pcDocLine ){
cEolCheck = pcDocLine->GetEol();
}
while( pcDocLine ){
CEol cEol = pcDocLine->GetEol();
if( cEol != cEolCheck && cEol != EOL_NONE ){
if( cEol != cEolCheck && cEol.IsValid() ){
bMix = true;
break;
}
Expand Down
92 changes: 46 additions & 46 deletions sakura_core/CEol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,16 @@
#include "StdAfx.h"
#include "CEol.h"

/*! 行終端子の配列 */
const EEolType gm_pnEolTypeArr[EOL_TYPE_NUM] = {
EOL_NONE , // == 0
EOL_CRLF , // == 2
EOL_LF , // == 1
EOL_CR , // == 1
EOL_NEL , // == 1
EOL_LS , // == 1
EOL_PS // == 1
};

//-----------------------------------------------
// 固定データ
//-----------------------------------------------

const SEolDefinition g_aEolTable[] = {
{ L"改行無", L"", "", 0 },
{ L"CRLF", L"\x0d\x0a", "\x0d\x0a", 2 },
{ L"CRLF", L"\x0d\x0a", "\x0d\x0a", 2 },
{ L"LF", L"\x0a", "\x0a", 1 },
{ L"CR", L"\x0d", "\x0d", 1 },
{ L"NEL", L"\x85", "", 1 },
{ L"NEL", L"\x85", "", 1 },
{ L"LS", L"\u2028", "", 1 },
{ L"PS", L"\u2029", "", 1 },
};
Expand Down Expand Up @@ -83,16 +72,17 @@ static const SEolDefinitionForUniFile g_aEolTable_uni_file[] = {
行終端子の種類を調べる。
@param pszData 調査対象文字列へのポインタ
@param nDataLen 調査対象文字列の長さ
@return 改行コードの種類。終端子が見つからなかったときはEOL_NONEを返す
@return 改行コードの種類。終端子が見つからなかったときはEEolType::noneを返す
*/
template <class T>
EEolType GetEOLType( const T* pszData, int nDataLen )
{
for( int i = 1; i < EOL_TYPE_NUM; ++i ){
if( g_aEolTable[i].StartsWith(pszData, nDataLen) )
return gm_pnEolTypeArr[i];
for( size_t i = 1; i < EOL_TYPE_NUM; ++i ){
if( g_aEolTable[i].StartsWith(pszData, nDataLen) ){
return static_cast<EEolType>(i);
}
}
return EOL_NONE;
return EEolType::none;
}

/*
Expand All @@ -101,54 +91,44 @@ EEolType GetEOLType( const T* pszData, int nDataLen )

EEolType _GetEOLType_uni( const char* pszData, int nDataLen )
{
for( int i = 1; i < EOL_TYPE_NUM; ++i ){
if( g_aEolTable_uni_file[i].StartsWithW(pszData, nDataLen) )
return gm_pnEolTypeArr[i];
for( size_t i = 1; i < EOL_TYPE_NUM; ++i ){
if( g_aEolTable_uni_file[i].StartsWithW(pszData, nDataLen) ){
return static_cast<EEolType>(i);
}
}
return EOL_NONE;
return EEolType::none;
}

EEolType _GetEOLType_unibe( const char* pszData, int nDataLen )
{
for( int i = 1; i < EOL_TYPE_NUM; ++i ){
if( g_aEolTable_uni_file[i].StartsWithWB(pszData, nDataLen) )
return gm_pnEolTypeArr[i];
for( size_t i = 1; i < EOL_TYPE_NUM; ++i ){
if( g_aEolTable_uni_file[i].StartsWithWB(pszData, nDataLen) ){
return static_cast<EEolType>(i);
}
}
return EOL_NONE;
return EEolType::none;
}

//-----------------------------------------------
// 実装部
//-----------------------------------------------

//! 現在のEOLの名称取得
const WCHAR* CEol::GetName() const
[[nodiscard]] LPCWSTR CEol::GetName() const noexcept
{
return g_aEolTable[ m_eEolType ].m_szName;
return g_aEolTable[static_cast<size_t>(m_eEolType)].m_szName;
}

//!< 現在のEOL文字列先頭へのポインタを取得
const wchar_t* CEol::GetValue2() const
//! 現在のEOL文字列先頭へのポインタを取得
[[nodiscard]] LPCWSTR CEol::GetValue2() const noexcept
{
return g_aEolTable[ m_eEolType ].m_szDataW;
return g_aEolTable[static_cast<size_t>(m_eEolType)].m_szDataW;
}

/*!
行末種別の設定。
@param t 行末種別
@retval true 正常終了。設定が反映された。
@retval false 異常終了。強制的にCRLFに設定。
*/
bool CEol::SetType( EEolType t )
//! 現在のEOL文字列長を取得。文字単位。
[[nodiscard]] CLogicInt CEol::GetLen() const noexcept
{
if( t < EOL_NONE || EOL_CODEMAX <= t ){
// 異常値
m_eEolType = EOL_CRLF;
return false;
}
// 正しい値
m_eEolType = t;
return true;
return CLogicInt(g_aEolTable[static_cast<size_t>(m_eEolType)].m_nLen);
}

void CEol::SetTypeByString( const wchar_t* pszData, int nDataLen )
Expand All @@ -170,3 +150,23 @@ void CEol::SetTypeByStringForFile_unibe( const char* pszData, int nDataLen )
{
SetType( _GetEOLType_unibe( pszData, nDataLen ) );
}

bool operator == ( const CEol& lhs, const CEol& rhs ) noexcept
{
return lhs.operator==(static_cast<EEolType>(rhs));
}

bool operator != ( const CEol& lhs, const CEol& rhs ) noexcept
{
return !(lhs == rhs);
}

bool operator == ( EEolType lhs, const CEol& rhs ) noexcept
{
return rhs.operator==(lhs);
}

bool operator != ( EEolType lhs, const CEol& rhs ) noexcept
{
return !(lhs == rhs);
}
129 changes: 84 additions & 45 deletions sakura_core/CEol.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,29 @@
#pragma once

#include "_main/global.h"
#include "basis/SakuraBasis.h"

// 2002/09/22 Moca EOL_CRLF_UNICODEを廃止
/* 行終端子の種類 */
enum EEolType : char {
EOL_NONE, //!<
EOL_CRLF, //!< 0d0a
EOL_LF, //!< 0a
EOL_CR, //!< 0d
EOL_NEL, //!< 85
EOL_LS, //!< 2028
EOL_PS, //!< 2029
EOL_CODEMAX, //
EOL_UNKNOWN = -1 //
/*!
行終端子の種類

行末記号の種類を定義する。
0より大きい値は、終端の種類に対応する。
ファイル末尾の行では「終端がない状態」があり得る。
ドキュメントの行末スタイルに合わせて自動付与を行うための値も定義しておく。

@date 2002/09/22 Moca EOL_CRLF_UNICODEを廃止
@date 2021/03/27 berryzplus 定数に意味のある名前を付ける
*/
enum class EEolType : char {
auto_detect = -1, //!< 行終端子の自動検出
none, //!< 行終端子なし
cr_and_lf, //!< \x0d\x0a 復帰改行
line_feed, //!< \x0a 改行
carriage_return, //!< \x0d 復帰
next_line, //!< \u0085 NEL
line_separator, //!< \u2028 LS
paragraph_separator, //!< \u2029 PS
code_max, //!< 範囲外検出用のマーカー(行終端子として使用しないこと)
};

struct SEolDefinition{
Expand All @@ -59,14 +69,8 @@ struct SEolDefinition{
bool StartsWith(const WCHAR* pData, int nLen) const{ return m_nLen<=nLen && 0==wmemcmp(pData,m_szDataW,m_nLen); }
bool StartsWith(const ACHAR* pData, int nLen) const{ return m_nLen<=nLen && m_szDataA[0] != '\0' && 0==memcmp(pData,m_szDataA,m_nLen); }
};
extern const SEolDefinition g_aEolTable[];

#define EOL_TYPE_NUM EOL_CODEMAX // 8

/* 行終端子の配列 */
extern const EEolType gm_pnEolTypeArr[EOL_TYPE_NUM];

#include "basis/SakuraBasis.h"
constexpr auto EOL_TYPE_NUM = static_cast<size_t>(EEolType::code_max); // 8
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

もとのコメント時点からそうですけど、これ8じゃなくて正確には7ですね、これ

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

大昔、 EOL_LFCR という定義があった時代には正しかったようです。
修正しても良かったですが、いったんそのままにしました。

なお、この定数はいずれ削除するつもりです。


/*!
@brief 行末の改行コードを管理
Expand All @@ -75,45 +79,80 @@ extern const EEolType gm_pnEolTypeArr[EOL_TYPE_NUM];
オブジェクトに対するメソッドで行えるだけだが、グローバル変数への参照を
クラス内部に閉じこめることができるのでそれなりに意味はあると思う。
*/
class CEol{
class CEol {
EEolType m_eEolType = EEolType::none; //!< 改行コードの種類

public:
//コンストラクタ・デストラクタ
CEol(){ m_eEolType = EOL_NONE; }
CEol( EEolType t ){ SetType(t); }
static constexpr bool IsNone( EEolType t ) noexcept
{
return t == EEolType::none;
}
static constexpr bool IsValid( EEolType t ) noexcept
{
return EEolType::none < t && t < EEolType::code_max;
}
static constexpr bool IsNoneOrValid( EEolType t ) noexcept
{
return IsNone( t ) || IsValid( t );
}

//比較
bool operator==( EEolType t ) const { return GetType() == t; }
bool operator!=( EEolType t ) const { return GetType() != t; }
constexpr explicit CEol( EEolType t ) noexcept
{
SetType( t );
}
CEol() noexcept = default;

//取得
[[nodiscard]] bool IsNone() const noexcept { return IsNone( m_eEolType ); } //!< 行終端子がないかどうか
[[nodiscard]] bool IsValid() const noexcept { return !IsNone(); } //!< 行終端子があるかどうか
[[nodiscard]] constexpr EEolType GetType() const noexcept { return m_eEolType; } //!< 現在のTypeを取得
[[nodiscard]] LPCWSTR GetName() const noexcept; //!< 現在のEOLの名称取得
[[nodiscard]] LPCWSTR GetValue2() const noexcept; //!< 現在のEOL文字列先頭へのポインタを取得
[[nodiscard]] CLogicInt GetLen() const noexcept; //!< 現在のEOL長を取得。文字単位。

//代入
const CEol& operator=( const CEol& t ){ m_eEolType = t.m_eEolType; return *this; }
//比較
[[nodiscard]] constexpr bool operator == ( EEolType t ) const noexcept { return GetType() == t; }
[[nodiscard]] constexpr bool operator != ( EEolType t ) const noexcept { return !operator == ( t ); }

//型変換
operator EEolType() const { return GetType(); }
[[nodiscard]] constexpr explicit operator EEolType() const { return GetType(); }

/*!
行末種別の設定。
@param t 行終端子の種別
@retval true 正常終了。設定が反映された。
@retval false 異常終了。強制的にCRLFに設定。
*/
constexpr bool SetType( EEolType t ) noexcept
{
if( IsNoneOrValid( t ) ){
// 正しい値
m_eEolType = t;
return true;
}else{
// 異常値
m_eEolType = EEolType::cr_and_lf;
return false;
}
}

//代入演算子
CEol& operator = ( EEolType t ) noexcept { SetType( t ); return *this; }

//設定
bool SetType( EEolType t); // Typeの設定
//文字列内の行終端子を解析
void SetTypeByString( const wchar_t* pszData, int nDataLen );
void SetTypeByString( const char* pszData, int nDataLen );

//設定(ファイル読み込み時に使用)
void SetTypeByStringForFile( const char* pszData, int nDataLen ){ SetTypeByString( pszData, nDataLen ); }
void SetTypeByStringForFile_uni( const char* pszData, int nDataLen );
void SetTypeByStringForFile_unibe( const char* pszData, int nDataLen );
};

//取得
EEolType GetType() const{ return m_eEolType; } //!< 現在のTypeを取得
CLogicInt GetLen() const { return CLogicInt(g_aEolTable[ m_eEolType ].m_nLen); } //!< 現在のEOL長を取得。文字単位。
const WCHAR* GetName() const; //!< 現在のEOLの名称取得
const wchar_t* GetValue2() const; //!< 現在のEOL文字列先頭へのポインタを取得
//#####

bool IsValid() const
{
return m_eEolType>=EOL_CRLF && m_eEolType<EOL_CODEMAX;
}
// グローバル演算子
bool operator == ( const CEol& lhs, const CEol& rhs ) noexcept;
bool operator != ( const CEol& lhs, const CEol& rhs ) noexcept;
bool operator == ( EEolType lhs, const CEol& rhs ) noexcept;
bool operator != ( EEolType lhs, const CEol& rhs ) noexcept;

private:
EEolType m_eEolType; //!< 改行コードの種類
};
#endif /* SAKURA_CEOL_036E1E16_7462_46A4_8F59_51D8E171E657_H_ */
2 changes: 1 addition & 1 deletion sakura_core/CSearchAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,7 @@ void CSearchAgent::ReplaceData( DocLineReplaceArg* pArg )
goto prev_line;
}
/* 改行も削除するんかぃのぉ・・・? */
if( EOL_NONE != pCDocLine->GetEol() &&
if( pCDocLine->GetEol().IsValid() &&
nWorkPos + nWorkLen > nLineLen - pCDocLine->GetEol().GetLen() // 2002/2/10 aroka CMemory変更
){
/* 削除する長さに改行も含める */
Expand Down
17 changes: 9 additions & 8 deletions sakura_core/charset/CCodeBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,14 @@ void CCodeBase::S_GetEol(CMemory* pcmemEol, EEolType eEolType)
int nLen;
}
aEolTable[EOL_TYPE_NUM] = {
{ "", 0 }, // EOL_NONE
{ "\x0d\x0a", 2 }, // EOL_CRLF
{ "\x0a", 1 }, // EOL_LF
{ "\x0d", 1 }, // EOL_CR
{ "", 0 }, // EOL_NEL
{ "", 0 }, // EOL_LS
{ "", 0 }, // EOL_PS
{ "", 0 }, // EEolType::none
{ "\x0d\x0a", 2 }, // EEolType::cr_and_lf
{ "\x0a", 1 }, // EEolType::line_feed
{ "\x0d", 1 }, // EEolType::carriage_return
{ "", 0 }, // EEolType::next_line
{ "", 0 }, // EEolType::line_separator
{ "", 0 }, // EEolType::paragraph_separator
};
pcmemEol->SetRawData(aEolTable[eEolType].szData,aEolTable[eEolType].nLen);
auto& data = aEolTable[static_cast<size_t>(eEolType)];
pcmemEol->SetRawData(data.szData, data.nLen);
}
2 changes: 1 addition & 1 deletion sakura_core/charset/CCodeBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#pragma once

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

//定数
enum EConvertResult{
Expand All @@ -37,7 +38,6 @@ enum EConvertResult{
};

struct CommonSetting_Statusbar;
enum EEolType : char;

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