CHANGED: #1471 moved some ucstring handling code to the NELGUI library ( required for CViewText )
--HG-- branch : gui-refactoringhg/feature/sse2
parent
b51dea4654
commit
1222e35012
@ -0,0 +1,29 @@
|
||||
#ifndef STRING_CASE_H
|
||||
#define STRING_CASE_H
|
||||
|
||||
#include "nel/misc/types_nl.h"
|
||||
#include "nel/misc/ucstring.h"
|
||||
|
||||
namespace NLGUI
|
||||
{
|
||||
|
||||
enum TCaseMode
|
||||
{
|
||||
CaseNormal = 0, // Nothing done
|
||||
CaseLower, // All letters in lowercase
|
||||
CaseUpper, // All letters in uppercase
|
||||
CaseFirstStringLetterUp, // The first letter of the string is uppercase, the others are lowercase
|
||||
CaseFirstSentenceLetterUp, // The first letter of the string and each sentences are uppercase, the others are lowercase. Sentences are seprated with '.'.
|
||||
CaseFirstWordLetterUp, // The first letter of each word is uppercase, the others are lowercase
|
||||
CaseCount
|
||||
};
|
||||
|
||||
|
||||
void setCase( ucstring &str, TCaseMode mode );
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
@ -0,0 +1,99 @@
|
||||
#include "nel/gui/string_case.h"
|
||||
|
||||
namespace NLGUI
|
||||
{
|
||||
inline bool isSeparator (ucchar c)
|
||||
{
|
||||
return (c == ' ') || (c == '\t') || (c == '\n') || (c == '\r');
|
||||
}
|
||||
|
||||
// ***************************************************************************
|
||||
|
||||
inline bool isEndSentence (ucstring& str, uint index)
|
||||
{
|
||||
// Ex: One sentence. Another sentence.
|
||||
// ^
|
||||
// Counterexample: nevrax.com
|
||||
// ^
|
||||
ucchar c = str[index];
|
||||
if ((str[index] == ' ') || (str[index] == '\n'))
|
||||
{
|
||||
if (index < 1)
|
||||
return false;
|
||||
c = str[index-1];
|
||||
return (c == '.') || (c == '!') || (c == '?');
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void setCase( ucstring &str, TCaseMode mode )
|
||||
{
|
||||
const uint length = (uint)str.length();
|
||||
uint i;
|
||||
bool newString = true;
|
||||
bool newSentence = true;
|
||||
bool newWord = true;
|
||||
switch (mode)
|
||||
{
|
||||
case CaseLower:
|
||||
str = NLMISC::toLower (str);
|
||||
break;
|
||||
case CaseUpper:
|
||||
str = NLMISC::toUpper (str);
|
||||
break;
|
||||
case CaseFirstStringLetterUp:
|
||||
for (i=0; i<length; i++)
|
||||
{
|
||||
if (!isSeparator (str[i]))
|
||||
{
|
||||
if (newString)
|
||||
str[i] = NLMISC::toUpper (str[i]);
|
||||
else
|
||||
str[i] = NLMISC::toLower (str[i]);
|
||||
newString = false;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case CaseFirstSentenceLetterUp:
|
||||
for (i=0; i<length; i++)
|
||||
{
|
||||
if (isEndSentence (str, i))
|
||||
newSentence = true;
|
||||
else
|
||||
{
|
||||
if (newSentence)
|
||||
str[i] = NLMISC::toUpper (str[i]);
|
||||
else
|
||||
str[i] = NLMISC::toLower (str[i]);
|
||||
|
||||
if (!isSeparator (str[i]))
|
||||
newSentence = false;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case CaseFirstWordLetterUp:
|
||||
for (i=0; i<length; i++)
|
||||
{
|
||||
if (isSeparator (str[i]) || isEndSentence (str, i))
|
||||
newWord = true;
|
||||
else
|
||||
{
|
||||
if (newWord)
|
||||
str[i] = NLMISC::toUpper (str[i]);
|
||||
else
|
||||
str[i] = NLMISC::toLower (str[i]);
|
||||
|
||||
newWord = false;
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue