Skip to content

Commit

Permalink
Small improvements on CSString and CSSortedVector libs.
Browse files Browse the repository at this point in the history
  • Loading branch information
cbnolok committed Oct 11, 2023
1 parent 6d7fac9 commit 06e8378
Show file tree
Hide file tree
Showing 14 changed files with 62 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/common/CDataBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ bool CDataBase::r_WriteVal(lpctstr ptcKey, CSString &sVal, CTextConsole *pSrc, b
{
ptcKey += strlen(sm_szLoadKeys[index]);
GETNONWHITESPACE(ptcKey);
sVal = "";
sVal.Clear();

if ( ptcKey[0] != '\0' )
{
Expand Down
2 changes: 1 addition & 1 deletion src/common/CPointBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ bool CPointBase::r_WriteVal( lpctstr ptcKey, CSString & sVal ) const
if ( pTypeDef != nullptr )
sVal = pTypeDef->GetResourceName();
else
sVal = "";
sVal.Clear();
} return true;
case PT_TERRAIN:
{
Expand Down
5 changes: 4 additions & 1 deletion src/common/CSFileObj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,10 @@ bool CSFileObj::r_WriteVal( lpctstr ptcKey, CSString &sVal, CTextConsole * pSrc,
} break;

case FO_FILEPATH:
sVal.Copy(_pFile->IsFileOpen() ? _pFile->GetFilePath() : "" );
if (_pFile->IsFileOpen())
sVal.Copy(_pFile->GetFilePath());
else
sVal.Clear();
break;

case FO_INUSE:
Expand Down
8 changes: 4 additions & 4 deletions src/common/CScriptObj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ bool CScriptObj::r_WriteVal( lpctstr ptcKey, CSString &sVal, CTextConsole * pSrc
if ( *ptcKey )
sVal = Str_TrimWhitespace(const_cast<tchar*>(ptcKey));
else
sVal = "";
sVal.Clear();

return true;
}
Expand Down Expand Up @@ -988,7 +988,7 @@ bool CScriptObj::r_WriteVal( lpctstr ptcKey, CSString &sVal, CTextConsole * pSrc
*iSeperator = '\0';

tchar *pArgs = Str_UnQuote(ppArgs[0]);
sVal = "";
sVal.Clear();
tchar *ppCmd[255];
int count = Str_ParseCmdsAdv(pArgs, ppCmd, ARRAY_COUNT(ppCmd), iSep); //Remove unnecessary chars from seperator to avoid issues.
tchar *ppArrays[2];
Expand Down Expand Up @@ -1042,7 +1042,7 @@ bool CScriptObj::r_WriteVal( lpctstr ptcKey, CSString &sVal, CTextConsole * pSrc
}

const char *p = ptcKey + strlen(separators) + 1;
sVal = "";
sVal.Clear();
if (( p > ptcKey ) && *p ) // we have list of accessible separators
{
tchar *ppCmd[255];
Expand Down Expand Up @@ -1717,7 +1717,7 @@ bool CScriptObj::Evaluate_QvalConditional(lpctstr ptcKey, CSString& sVal, CTextC

sVal = ptcTemp;
if (sVal.IsEmpty())
sVal = "";
sVal.Clear();
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/common/CScriptTriggerArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ bool CScriptTriggerArgs::r_WriteVal( lpctstr ptcKey, CSString &sVal, CTextConsol
uint uiNum = Exp_GetUSingle(ptcKey);
if ( uiNum >= m_v.size() )
{
sVal = "";
sVal.Clear();
return true;
}
sVal = m_v[uiNum];
Expand Down
2 changes: 1 addition & 1 deletion src/common/ListDefContMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ void CListDefCont::PrintElements(CSString& strElements) const
ADDTOCALLSTACK("CListDefCont::PrintElements");
if ( m_listElements.empty() )
{
strElements = "";
strElements.Clear();
return;
}

Expand Down
4 changes: 2 additions & 2 deletions src/common/sphere_library/CSSortedVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ size_t CSSortedVector<_Type, _Comp>::find(_Type const& value) const noexcept
return SCONT_BADINDEX;
}
const _Type* const _dataptr = this->data();
const size_t _idx = this->lower_element(_mySize, _dataptr, value);
size_t _idx = this->lower_element(_mySize, _dataptr, value);
if (_idx == _mySize)
return _mySize - 1;
return SCONT_BADINDEX;
return (!this->_comparatorObj(value, _dataptr[_idx])) ? _idx : SCONT_BADINDEX;
}

Expand Down
26 changes: 23 additions & 3 deletions src/common/sphere_library/CSString.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ class CSString
/**
* @brief Returns an empty string.
*/
[[maybe_unused]]
inline static CSString Empty();
[[maybe_unused, nodiscard]]
inline static CSString EmptyNew();

/**
* @brief Copy supplied string into the CSString.
Expand Down Expand Up @@ -129,12 +129,14 @@ class CSString
* @brief Check the length of the CSString.
* @return true if length is 0, false otherwise.
*/
NODISCARD
inline bool IsEmpty() const noexcept;

/**
* @brief Check if there is data allocated and if has zero length.
* @return false if no data or zero length, true otherwise.
*/
NODISCARD
bool IsValid() const noexcept;

/**
Expand All @@ -152,12 +154,14 @@ class CSString
* @brief Get the length of the held string.
* @return the length of the CSString.
*/
NODISCARD
inline int GetLength() const noexcept;

/**
* @brief Get the length of the internal buffer, which can be greater than the held string length.
* @return the length of the CSString.
*/
NODISCARD
inline int GetCapacity() const noexcept;

///@}
Expand All @@ -172,6 +176,7 @@ class CSString
* @param nIndex position of the character.
* @return character in position nIndex.
*/
NODISCARD
inline tchar operator[](int nIndex) const;

/**
Expand All @@ -180,20 +185,23 @@ class CSString
* @param nIndex position of the character.
* @return reference to character in position nIndex.
*/
NODISCARD
inline tchar& operator[](int nIndex);

/**
* @brief Gets the caracter in a specified position (0 based).
* @param nIndex position of the character.
* @return character in position nIndex.
*/
NODISCARD
inline tchar GetAt(int nIndex) const;

/**
* @brief Gets the reference to character a specified position (0 based).
* @param nIndex position of the character.
* @return reference to character in position nIndex.
*/
NODISCARD
inline tchar& ReferenceAt(int nIndex);

/**
Expand Down Expand Up @@ -469,6 +477,7 @@ class CSString
* @brief cast as const lpcstr.
* @return internal data pointer.
*/
NODISCARD
inline operator lpctstr() const noexcept;

/**
Expand All @@ -481,6 +490,7 @@ class CSString
* @param pStr string to compare.
* @return <0 if the first character that not match has lower value in CSString than in pStr. 0 if the contents of both are equal. >0 if the first character that does not match has greater value in CSString than pStr.
*/
NODISCARD
inline int Compare(lpctstr pStr) const noexcept;

/**
Expand All @@ -493,12 +503,14 @@ class CSString
* @param pStr string to compare.
* @return <0 if the first character that not match has lower value in CSString than in pStr. 0 if the contents of both are equal. >0 if the first character that does not match has greater value in CSString than pStr.
*/
NODISCARD
inline int CompareNoCase(lpctstr pStr) const noexcept;

/**
* @brief Gets the internal pointer.
* @return Pointer to internal data.
*/
NODISCARD
inline lpctstr GetBuffer() const noexcept;

// Provide only a read-only buffer: if we modify it we'll break the internal length counter, other than possibly write past the end of the string (the buffer is small).
Expand All @@ -509,6 +521,7 @@ class CSString
* @param c character to look for.
* @return position of the character in CSString if any, -1 otherwise.
*/
NODISCARD
inline int indexOf(tchar c) noexcept;

/**
Expand All @@ -517,13 +530,15 @@ class CSString
* @param offset position from start the search.
* @return position of the character in CSString if any, -1 otherwise.
*/
NODISCARD
int indexOf(tchar c, int offset) noexcept;

/**
* @brief Look for the first occurence of a substring in CSString.
* @param str substring to look for.
* @return position of the substring in CSString if any, -1 otherwise.
*/
NODISCARD
inline int indexOf(const CSString& str) noexcept;

/**
Expand All @@ -532,13 +547,15 @@ class CSString
* @param offset position from start the search.
* @return position of the substring in CSString if any, -1 otherwise.
*/
NODISCARD
int indexOf(const CSString& str, int offset) noexcept;

/**
* @brief Look for the last occurence of c in CSString.
* @param c character to look for.
* @return position of the character in CSString if any, -1 otherwise.
*/
NODISCARD
inline int lastIndexOf(tchar c) noexcept;

/**
Expand All @@ -547,13 +564,15 @@ class CSString
* @param from position where stop the search.
* @return position of the character in CSString if any, -1 otherwise.
*/
NODISCARD
int lastIndexOf(tchar c, int from) noexcept;

/**
* @brief Look for the last occurence of a substring in CSString.
* @param str substring to look for.
* @return position of the substring in CSString if any, -1 otherwise.
*/
NODISCARD
inline int lastIndexOf(const CSString& str) noexcept;

/**
Expand All @@ -562,6 +581,7 @@ class CSString
* @param from position where stop the search.
* @return position of the substring in CSString if any, -1 otherwise.
*/
NODISCARD
int lastIndexOf(const CSString& str, int from) noexcept;

///@}
Expand All @@ -584,7 +604,7 @@ CSString::CSString(CSString&& s) noexcept :
*this = std::move(s); // Call the move assignment operator
}

CSString CSString::Empty()
CSString CSString::EmptyNew()
{
return CSString(false);
}
Expand Down
2 changes: 1 addition & 1 deletion src/game/CRegion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ bool CRegion::r_WriteVal( lpctstr ptcKey, CSString & sVal, CTextConsole * pSrc,
if (pBase != nullptr)
sVal = pBase->GetResourceName();
else
sVal = "";
sVal.Clear();
} break;
case RC_UID:
// Allow use of UID.x.KEY on the REGION object
Expand Down
14 changes: 10 additions & 4 deletions src/game/chars/CChar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2756,8 +2756,11 @@ bool CChar::r_WriteVal( lpctstr ptcKey, CSString & sVal, CTextConsole * pSrc, bo
return true;
case CHC_GUILDABBREV:
{
lpctstr pszAbbrev = Guild_Abbrev(MEMORY_GUILD);
sVal = ( pszAbbrev ) ? pszAbbrev : "";
lpctstr ptcAbbrev = Guild_Abbrev(MEMORY_GUILD);
if (ptcAbbrev)
sVal = ptcAbbrev;
else
sVal.Clear();
}
return true;
case CHC_ID:
Expand Down Expand Up @@ -2867,8 +2870,11 @@ bool CChar::r_WriteVal( lpctstr ptcKey, CSString & sVal, CTextConsole * pSrc, bo
break;
case CHC_TOWNABBREV:
{
lpctstr pszAbbrev = Guild_Abbrev(MEMORY_TOWN);
sVal = ( pszAbbrev ) ? pszAbbrev : "";
lpctstr ptcAbbrev = Guild_Abbrev(MEMORY_TOWN);
if (ptcAbbrev)
sVal = ptcAbbrev;
else
sVal.Clear();
}
return true;
case CHC_MAXWEIGHT:
Expand Down
6 changes: 5 additions & 1 deletion src/game/clients/CParty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -642,12 +642,16 @@ bool CPartyDef::r_WriteVal( lpctstr ptcKey, CSString &sVal, CTextConsole *pSrc,
break;

case PDC_SPEECHFILTER:
sVal = this->m_pSpeechFunction.IsEmpty() ? "" : this->m_pSpeechFunction;
if (m_pSpeechFunction.IsEmpty())
sVal.Clear();
else
sVal = m_pSpeechFunction;
break;

case PDC_TAG0:
fZero = true;
++ptcKey;
FALLTHROUGH;
case PDC_TAG:
{
if ( ptcKey[3] != '.' )
Expand Down
5 changes: 4 additions & 1 deletion src/game/items/CItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2612,7 +2612,10 @@ bool CItem::r_WriteVal( lpctstr ptcKey, CSString & sVal, CTextConsole * pSrc, bo
case IC_SUMMONING:
{
const CVarDefCont * pVar = GetDefKey(ptcKey, true);
sVal = pVar ? pVar->GetValStr() : "";
if (pVar)
sVal = pVar->GetValStr();
else
sVal.Clear();
}
break;
//return as decimal number or 0 if not set
Expand Down
4 changes: 2 additions & 2 deletions src/game/items/CItemStone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ bool CItemStone::r_WriteVal( lpctstr ptcKey, CSString & sVal, CTextConsole * pSr
lpctstr pszCmd = ptcKey + 7;
uint i = atoi(pszCmd);
if ( i >= ARRAY_COUNT(m_sCharter))
sVal = "";
sVal.Clear();
else
sVal = m_sCharter[i];

Expand Down Expand Up @@ -723,7 +723,7 @@ bool CItemStone::r_WriteVal( lpctstr ptcKey, CSString & sVal, CTextConsole * pSr
{
CChar * pMaster = GetMaster();
if ( pMaster == nullptr )
sVal = ""; // If no master (vote pending)
sVal.Clear(); // If no master (vote pending)
else if ( pMaster->Char_GetDef()->IsFemale())
sVal = g_Exp.m_VarDefs.GetKeyStr("STONECONFIG_VARIOUSNAME_MASTERGENDERFEMALE");
else
Expand Down
4 changes: 3 additions & 1 deletion src/network/CNetworkInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,9 @@ bool CNetworkInput::processGameClientData(CNetState* state, Packet* buffer)
state->m_packetExceptions++;
if (state->m_packetExceptions > 10)
{
g_Log.Event(LOGM_CLIENTS_LOG | LOGL_WARN, "%x:Disconnecting client from account '%s' since it is causing exceptions problems\n", state->id(), client != nullptr && client->GetAccount() ? client->GetAccount()->GetName() : "");
g_Log.Event(LOGM_CLIENTS_LOG | LOGL_WARN,
"%x:Disconnecting client from account '%s' since it is causing exceptions problems\n",
state->id(), client != nullptr && client->GetAccount() ? client->GetAccount()->GetName() : "");
if (client != nullptr)
client->addKick(&g_Serv, false);
else
Expand Down

0 comments on commit 06e8378

Please sign in to comment.