Pour la gestion des strings...
//::///////////////////////////////////////////////
//:: Gestion avancee des chaines de caracteres
//:: inc_strings.nss
//:: Copyright (c) Althea 2002
//:://////////////////////////////////////////////
/*
Bibliotheques de fonction de traitement
avance des chaines de caracteres.
*/
//:://////////////////////////////////////////////
//:: Créé par : Tynril
//:: Créé le : 21/09/2002
//:://////////////////////////////////////////////
//----------------------------------------------------------------------------
// CONSTANTES
// Succes de l'operation sur le Tokenizer.
int StrTOKENIZER_SUCCESS = 0;
// Erreur retournee si l'on essaye d'initialise un Tokenizer existant.
int StrTOKENIZER_EXISTS = -1;
// Erreur retournee si l'on essaye d'utiliser un Tokenizer non-initialise.
int StrTOKENIZER_NOTINITIALIZED = -2;
// Erreur retournee si l'on essaye d'utiliser un Tokenizer avec un Token vide.
int StrTOKENIZER_EMPTYTOKEN = -3;
// Erreur retournee si l'on essaye d'utiliser un Tokenizer avec une chaine vide.
int StrTOKENIZER_EMPTYSTRING = -4;
//----------------------------------------------------------------------------
// PROTOTYPES DE FONCTIONS ET DECLARATIONS
// Retourne les caracteres se trouvant avant sPattern.
// Exemple : StrBefore("Un chien"," ") -> "Un"
// Si sPattern n'est pas trouve dans la chaine, celle-ci est retournee entiere.
string StrBefore(string sSource, string sPattern);
// Retourne les caracteres se trouvant apres sPattern.
// Exemple : StrAfter("Un chien"," ") -> "chien"
// Si sPattern n'est pas trouve dans la chaine, celle-ci est retournee entiere.
string StrAfter(string sSource, string sPattern);
// Initialize the string tokenizer with a given string and token. Optionally,
// takes a tokenizer session, in case you need to run more than one tokenizer
// at a time. The tokenized string can be walked with the function
// StrTokenizerNext, and freed with StrTokenizerFree.
//
int StrTokenizerInitialize(string sStringToTokenize, string sToken, int iTokenizerSession = 0);
// Returns the next token-delimited string from a given initialized tokenizer,
// or "" if the tokenizer is not initialized or empty of tokens. A tokenizer
// needs to be allocated with Strtokenizer_initialize and freed with
// Strtokenizer_free.
string StrTokenizerNext(int iTokenizerSession = 0);
// Frees an initialized tokenizer.
int StrTokenizerFree(int iTokenizerSession = 0);
// Turns a given string uppercase
string StrUpperCase(string sSource);
// Turns a given string lowercase
string StrLowerCase(string sSource);
// Replaces all instances of sReplaceMe in sSource with sNewText, and return
// the altered string. If sReplaceMe is empty, then sSource is immediately
// returned unaltered.
string StrReplace(string sSource, string sReplaceMe, string sNewText);
// Capitalize the first letter of a string. Useful for turning
// 'mister' into 'Mister', for example, in generating conversation or
// other text.
//
string StrCapStr(string sSource);
// Pads a string on the left with the pattern given.
string StrPadLeft(string sOriginal, int iPadTo, string sPadWith);
// Pads a string on the right with the pattern given.
string StrPadRight(string sOriginal, int iPadTo, string sPadWith);
// Returns the member position of the token in the string, beginning with 0,
// or -1 if the given token is not a member of the string list. Token must
// be a complete one.
//
int StrMember(string sList, string sToken, string sDelimit = " ", int iTokenizer = 969);
// Returns the number of tokens in a delimited list string, or -1 on error.
int StrMemberCount(string sList, string sDelimit = " ", int iTokenizer = 969);
//----------------------------------------------------------------------------
// ACTUAL FUNCTION SOURCES
// FUNCTION: StrBefore
//
// Returns the portion of sSource which is before the first occurance of
// sPattern. For example, Strbefore("Foo bar"," ") -> "Foo"
//
// If the pattern does not exist in the string, the entire string is returned.
string StrBefore(string sSource, string sPattern)
{
int iMarker;
string sResult;
// Sanity check!
if ((sPattern == "") || (sSource == ""))
return sSource;
iMarker = FindSubString(sSource,sPattern);
if (iMarker >= 0) {
sResult = GetSubString(sSource,0,iMarker);
}
else {
sResult = sSource;
}
return sResult;
}
// FUNCTION: StrAfter
//
// Returns the portion of sSource which is after the first occurance of
// sPattern. For example, Strbefore("Foo bar"," ") -> "bar"
//
// If the pattern does not exist in the string, an empty string is returned.
string StrAfter(string sSource, string sPattern)
{
int iMarker, iMarkerLength;
string sResult;
// Sanity check!
if ((sPattern == "") || (sSource == ""))
return "";
iMarkerLength = GetStringLength(sPattern);
iMarker = FindSubString(sSource,sPattern);
if (iMarker >= 0) {
sResult = GetSubString(sSource,iMarker + iMarkerLength,
GetStringLength(sSource) - (iMarker + iMarkerLength));
}
else {
sResult = "";
}
return sResult;
}
// FUNCTION: StrTokenizerInitialize
//
// Initialize the string tokenizer with a given string and token. Optionally,
// takes a tokenizer session, in case you need to run more than one tokenizer
// at a time. The tokenized string can be walked with the function
// Strtokenizer_next, and freed with Strtokenizer_free.
//
int StrTokenizerInitialize(string sStringToTokenize, string sToken,
int iTokenizerSession = 0)
{
// Sanity check to make sure we're not clobbering an existing token.
if (GetLocalString(OBJECT_SELF,"tokenizer_" + IntToString(iTokenizerSession) + "_token") != "") {
return StrTOKENIZER_EXISTS;
}
// Sanity check that we have a token.
if (sToken == "")
return StrTOKENIZER_EMPTYTOKEN;
// Sanity check that we have a string to tokenize!
if (sStringToTokenize == "")
return StrTOKENIZER_EMPTYSTRING;
// Set up our data for later.
SetLocalString(OBJECT_SELF,
"tokenizer_" + IntToString(iTokenizerSession) + "_token",
sToken);
SetLocalString(OBJECT_SELF,
"tokenizer_" + IntToString(iTokenizerSession) + "_stringbuf",
sStringToTokenize);
return StrTOKENIZER_SUCCESS;
}
// FUNCTION: StrTokenizerNext
//
// Returns the next token-delimited string from a given initialized tokenizer,
// or "" if the tokenizer is not initialized or empty of tokens. A tokenizer
// needs to be allocated with Strtokenizer_initialize and freed with
// Strtokenizer_free.
//
string StrTokenizerNext(int iTokenizerSession = 0)
{
string sCurBuffer, sNewBuffer, sToken;
sToken = GetLocalString(OBJECT_SELF, "tokenizer_" +
IntToString(iTokenizerSession) + "_token");
sCurBuffer = GetLocalString(OBJECT_SELF, "tokenizer_" +
IntToString(iTokenizerSession) + "_stringbuf");
// Sanity check to make sure we have an initialized tokenizer
if (sToken == "")
return "";
sNewBuffer = StrBefore(sCurBuffer, sToken);
sCurBuffer = StrAfter(sCurBuffer, sToken);
SetLocalString(OBJECT_SELF, "tokenizer_" +
IntToString(iTokenizerSession) + "_stringbuf", sCurBuffer);
return sNewBuffer;
}
// FUNCTION: StrTokenizerFree
//
// Frees an initialized tokenizer.
//
int StrTokenizerFree(int iTokenizerSession = 0)
{
if (GetLocalString(OBJECT_SELF, "tokenizer_" +
IntToString(iTokenizerSession) + "_token") == "")
return StrTOKENIZER_NOTINITIALIZED;
DeleteLocalString(OBJECT_SELF, "tokenizer_" +
IntToString(iTokenizerSession) + "_token");
DeleteLocalString(OBJECT_SELF, "tokenizer_" +
IntToString(iTokenizerSession) + "_stringbuf");
return StrTOKENIZER_SUCCESS;
}
// FUNCTION: StrUpperCase
//
// Turns a given string uppercase
//
string StrUpperCase(string sSource)
{
string sResult;
string UppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string LowercaseLetters = "abcdefghijklmnopqrstuvwxyz";
int loop, marker;
// Sanity check!
if (sSource == "")
return "";
// Walk the string...
for (loop = 0; loop < GetStringLength(sSource); loop++) {
string sChar;
int marker;
// Get the current character of the string...
sChar = GetSubString(sSource, loop, 1);
// ...and see if it's in the LowercaseLetter set.
marker = FindSubString(LowercaseLetters, sChar);
if (marker >= 0) {
// If so, replace with a matching UppercaseLetter.
sChar = GetSubString(UppercaseLetters, marker, 1);
}
// Append the current character to the result.
sResult = sResult + sChar;
}
return sResult;
}
// FUNCTION: StrLowerCase
//
// Turns a given string lowercase
//
string StrLowerCase(string sSource)
{
string sResult;
string UppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string LowercaseLetters = "abcdefghijklmnopqrstuvwxyz";
int loop, marker;
// Sanity check!
if (sSource == "")
return "";
// Walk the string...
for (loop = 0; loop < GetStringLength(sSource); loop++) {
string sChar;
int marker;
// Get the current character of the string...
sChar = GetSubString(sSource, loop, 1);
// ...and see if it's in the UppercaseLetter set.
marker = FindSubString(UppercaseLetters, sChar);
if (marker >= 0) {
// If so, replace with a matching LowercaseLetter.
sChar = GetSubString(LowercaseLetters, marker, 1);
}
// Append the current character to the result.
sResult = sResult + sChar;
}
return sResult;
}
// FUNCTION: StrReplace
//
// Replaces all instances of sReplaceMe in sSource with sNewText, and return
// the altered string. If sReplaceMe is empty, then sSource is immediately
// returned unaltered.
//
string StrReplace(string sSource, string sReplaceMe, string sNewText)
{
string sResult;
int marker;
// Sanity check!
if (sSource == "")
return "";
// Sanity check!
if (sReplaceMe == "")
return sSource;
sResult = sSource;
// As long as an instance of our replace-string exists, keep walking
// the string and altering it.
while ((marker = FindSubString(sResult, sReplaceMe)) >= 0) {
// Isn't it nice to be able to reuse your own library functions?
sResult =
StrBefore(sResult, sReplaceMe) +
sNewText +
StrAfter(sResult, sReplaceMe);
}
return sResult;
}
// FUNCTION: StrCapStr
//
// Capitalize the first letter of a string. Useful for turning
// 'mister' into 'Mister', for example, in generating conversation or
// other text.
//
string StrCapStr(string sSource)
{
string sResult;
string UppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string LowercaseLetters = "abcdefghijklmnopqrstuvwxyz";
string sChar;
int iPos;
sChar = GetSubString(sSource, 0, 1);
iPos = FindSubString(LowercaseLetters, sChar);
if (iPos != -1) {
sChar = GetSubString(UppercaseLetters,iPos,1);
}
sResult = sChar + GetSubString(sSource,1,GetStringLength(sSource) - 1);
return sResult;
}
// FUNCTION: StrPadLeft
//
// Pads a string on the left with the pattern given.
string StrPadLeft(string sOriginal, int iPadTo, string sPadWith)
{
string sNew;
sNew = sOriginal;
while (GetStringLength(sNew) < iPadTo) {
sNew = sPadWith + sNew;
}
if (GetStringLength(sNew) > iPadTo) {
sNew = GetStringRight(sNew, iPadTo);
}
return sNew;
}
// FUNCTION: StrPadRight
//
// Pads a string on the right with the pattern given.
string StrPadRight(string sOriginal, int iPadTo, string sPadWith)
{
string sNew;
sNew = sOriginal;
while (GetStringLength(sNew) < iPadTo) {
sNew = sNew + sPadWith;
}
if (GetStringLength(sNew) > iPadTo) {
sNew = GetStringLeft(sNew, iPadTo);
}
return sNew;
}
// FUNCTION: StrMember
//
// Returns the member position of the token in the string, beginning with 0,
// or -1 if the given token is not a member of the string list. Token must
// be a complete one.
//
int StrMember(string sList, string sToken, string sDelimit = " ", int iTokenizer = 969)
{
string sTempKey;
int iCounter, iFound;
iCounter = 0;
iFound = -1;
if (StrTokenizerInitialize(sList, sDelimit, iTokenizer) != StrTOKENIZER_SUCCESS) {
return -1;
}
while (((sTempKey = StrTokenizerNext(iTokenizer)) != "") && (iFound == -1)) {
if (sTempKey == sToken) {
iFound = iCounter;
}
iCounter++;
}
StrTokenizerFree(iTokenizer);
return iFound;
}
// FUNCTION: StrMemberCount
//
// Returns the number of tokens in a delimited list string, or -1 on error.
//
int StrMemberCount(string sList, string sDelimit = " ", int iTokenizer = 969)
{
string sTempKey;
int iCounter;
iCounter = 0;
if (sList == "")
return 0;
if (FindSubString(sList, sDelimit) == -1)
return 0;
if (StrTokenizerInitialize(sList, sDelimit, iTokenizer) != StrTOKENIZER_SUCCESS) {
return -1;
}
while ((sTempKey = StrTokenizerNext(iTokenizer)) != "") {
iCounter++;
}
StrTokenizerFree(iTokenizer);
return iCounter;
}