Skip to content

Commit 052675b

Browse files
committed
Move Div_Ceil to separate function.
1 parent 1b12cad commit 052675b

File tree

4 files changed

+17
-9
lines changed

4 files changed

+17
-9
lines changed

Core/GameEngine/Include/Common/AsciiString.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -385,9 +385,8 @@ class AsciiString
385385

386386
Char& operator[](Int index)
387387
{
388-
Int length = getLength();
389-
DEBUG_ASSERTCRASH(index >= 0 && index < length, ("bad index in AsciiString::operator[]"));
390-
ensureUniqueBufferOfSize(length + 1, true, NULL, NULL);
388+
DEBUG_ASSERTCRASH(index >= 0 && index < getLength(), ("bad index in AsciiString::operator[]"));
389+
ensureUniqueBufferOfSize(m_data->m_numCharsAllocated, true, NULL, NULL);
391390
return peek()[index];
392391
}
393392

Core/GameEngine/Include/Common/UnicodeString.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,7 @@ class UnicodeString
337337

338338
WideChar& operator[](Int index)
339339
{
340-
DEBUG_ASSERTCRASH(m_data && m_data->m_numCharsAllocated > index && index >= 0, ("bad index in UnicodeString::operator[]"));
341-
340+
DEBUG_ASSERTCRASH(index >= 0 && index < getLength(), ("bad index in UnicodeString::operator[]"));
342341
ensureUniqueBufferOfSize(m_data->m_numCharsAllocated, true, NULL, NULL);
343342
return peek()[index];
344343
}

Core/GameEngine/Source/GameNetwork/GameInfo.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -959,22 +959,20 @@ static Bool TruncatePlayerNames(AsciiStringVec& playerNames, Int truncateAmount)
959959
// sort based on length in ascending order
960960
std::sort(lengthIndex.begin(), lengthIndex.end());
961961

962-
Int truncateNameAmount = 0;
963962
for (size_t i = 0; i < lengthIndex.size(); ++i)
964963
{
965964
const Int playerIndex = lengthIndex[i].Index;
966965
const Int playerLength = lengthIndex[i].Length;
967966

968967
// round avg name length up, which will penalize the final entry (longest name) as it will have to account for the roundings
969-
const Int avgNameLength = ((remainingNamesLength - truncateAmount) + (playerNames.size() - i - 1)) / (playerNames.size() - i);
968+
const Int avgNameLength = WWMath::Div_Ceil((remainingNamesLength - truncateAmount), (playerNames.size() - i));
970969
remainingNamesLength -= playerLength;
971970
if (playerLength <= avgNameLength)
972971
{
973972
continue;
974973
}
975974

976-
// ensure a longer name is not truncated less than a previous, shorter name
977-
truncateNameAmount = std::max(truncateNameAmount, playerLength - avgNameLength);
975+
Int truncateNameAmount = playerLength - avgNameLength;
978976
if (i == lengthIndex.size() - 1)
979977
{
980978
// ensure we account for rounding errors when truncating the last, longest entry

Core/Libraries/Source/WWVegas/WWMath/wwmath.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ static WWINLINE bool Is_Valid_Double(double x);
168168

169169
static WWINLINE float Normalize_Angle(float angle); // Normalizes the angle to the range -PI..PI
170170

171+
static WWINLINE int Div_Ceil(const int num, const int den);
172+
171173
};
172174

173175
WWINLINE float WWMath::Sign(float val)
@@ -654,3 +656,13 @@ WWINLINE float WWMath::Normalize_Angle(float angle)
654656
{
655657
return angle - (WWMATH_TWO_PI * Floor((angle + WWMATH_PI) / WWMATH_TWO_PI));
656658
}
659+
660+
// ----------------------------------------------------------------------------
661+
// Ceil rounded int division
662+
// Rounding away from 0 for positive values, towards 0 for negative values
663+
// ----------------------------------------------------------------------------
664+
WWINLINE int WWMath::Div_Ceil(const int num, const int den)
665+
{
666+
const div_t res = std::div(num, den);
667+
return (res.rem != 0 && res.quot >= 0) ? res.quot + 1 : res.quot;
668+
}

0 commit comments

Comments
 (0)