Skip to content

Commit

Permalink
TTString: initial move back to being an alias of std::string. see #339.
Browse files Browse the repository at this point in the history
  • Loading branch information
timothy place committed Mar 3, 2015
1 parent eaf08f6 commit 5e85fd3
Show file tree
Hide file tree
Showing 13 changed files with 115 additions and 98 deletions.
6 changes: 3 additions & 3 deletions Foundation/library/JamomaFoundation.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -455,16 +455,16 @@
2209F8FF1AA0E67A00FB8D49 /* TTRegex.h */,
2209F8AF1AA0E66800FB8D49 /* TTRegex.cpp */,
2209F9001AA0E67A00FB8D49 /* TTString.h */,
2209F8B01AA0E66800FB8D49 /* TTString.cpp */,
2209F9131AA0E6A600FB8D49 /* TTString.test.cpp */,
2209F9141AA0E6A600FB8D49 /* TTString.test.h */,
2209F9011AA0E67A00FB8D49 /* TTSymbol.h */,
2209F9021AA0E67A00FB8D49 /* TTSymbolBase.h */,
2209F8B11AA0E66800FB8D49 /* TTSymbolBase.cpp */,
2209F9031AA0E67A00FB8D49 /* TTSymbolCache.h */,
2209F9041AA0E67A00FB8D49 /* TTSymbolTable.h */,
2209F8B01AA0E66800FB8D49 /* TTString.cpp */,
2209F8B21AA0E66800FB8D49 /* TTSymbolCache.cpp */,
2209F8B31AA0E66800FB8D49 /* TTSymbolTable.cpp */,
2209F9131AA0E6A600FB8D49 /* TTString.test.cpp */,
2209F9141AA0E6A600FB8D49 /* TTString.test.h */,
2209F9151AA0E6A600FB8D49 /* TTSymbol.test.cpp */,
2209F9161AA0E6A600FB8D49 /* TTSymbol.test.h */,
);
Expand Down
9 changes: 6 additions & 3 deletions Foundation/library/includes/TTAddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,14 @@ class TTFOUNDATION_EXPORT TTAddress : public TTSymbol
}


TTAddress(const TTString& s) : TTAddress(s.c_str()) {}
TTAddress(const TTSymbol& s) : TTAddress(s.c_str()) {}


// TODO: address code duplication between TTAddress and TTSymbol
TTAddress(const int int_to_convert_to_string)
{
TTString s;

s.append(int_to_convert_to_string);
TTString s = std::to_string(int_to_convert_to_string);
mSymbolPointer = gTTAddressTable.lookup(s);
}

Expand Down
2 changes: 1 addition & 1 deletion Foundation/library/includes/TTElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ class TTFOUNDATION_EXPORT TT_ALIGN_16 TTElement {
//if (mType == kTypeAddress)
// return *mValue.mAddress;
if (mType == kTypeSymbol)
return TTAddress(*mValue.mSymbol);
return TTAddress(*mValue.mSymbol->c_str());
else
return kTTAdrsEmpty;
}
Expand Down
6 changes: 3 additions & 3 deletions Foundation/library/includes/TTRegex.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ class TTFOUNDATION_EXPORT TTRegex {
@param begin the beginning of the string to parse
@param end the end of the string to parse
@return a error code */
TTErr parse(TTStringIter& begin, TTStringIter& end);
TTErr parse(TTString::iterator& begin, TTString::iterator& end);


/** Get where start the result */
//TTRegexStringPosition begin();
TTStringIter begin();
TTString::iterator begin();

/** Get where end the result */
//TTRegexStringPosition end();
TTStringIter end();
TTString::iterator end();
};


Expand Down
58 changes: 23 additions & 35 deletions Foundation/library/includes/TTString.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,32 @@
http://creativecommons.org/licenses/BSD/
*/

#pragma once

//#include <string>
#include "TTBase.h"

using TTString = std::string;

// NOTE: we do not export TTString because it is defined in a header as a subclass of a stl template
// but we do want to export this method, which is not defined inline so that we don't pick up a direct
// dependency on Mersenne Twister
/** Replace contents with a pseudo-random string. */
std::string TTFOUNDATION_EXPORT TTGenerateRandomString();

// TODO: These conversions seem a bit misguided, but are being maintained momentarily for backward compatibility.
// Should be replaced with standard calls like std::stoi() and friends.

TTBoolean TTFOUNDATION_EXPORT TTStringToTTInt32(const std::string s, TTInt32& convertedInt);
TTBoolean TTFOUNDATION_EXPORT TTStringToTTUInt32(const std::string s, TTUInt32& convertedUInt);
TTBoolean TTFOUNDATION_EXPORT TTStringToTTFloat32(const std::string s, TTFloat32& convertedFloat);


#define __TT_STRING_H__

#ifndef __TT_STRING_H__
#define __TT_STRING_H__

#include "TTBase.h"


/****************************************************************************************************/
Expand Down Expand Up @@ -369,42 +391,8 @@ class TTString : public std::vector<char> {
}


// NOTE: we do not export TTString because it is defined in a header as a subclass of a stl template
// but we do want to export this method, which is not defined inline so that we don't pick up a direct
// dependency on Mersenne Twister
/** Replace contents with a pseudo-random string. */
void TTFOUNDATION_EXPORT random();


TTBoolean toTTInt32(TTInt32& convertedInt) const
{
char * pEnd;

convertedInt = (TTInt32)strtol(c_str(), &pEnd, 10);
return *pEnd == 0;
}


TTBoolean toTTUInt32(TTUInt32& convertedUInt) const
{
char * pEnd;

convertedUInt = (TTUInt32)strtoul(c_str(), &pEnd, 10);

// is the last letter is a 'u' ?
return *pEnd == 'u' && pEnd == (c_str() + length() - 1);
}

/* note : isTTFloat32 works only because the TTInt32 case is matched before
see in TTValue::fromString method
*/
TTBoolean toTTFloat32(TTFloat32& convertedFloat) const
{
char * pEnd;

convertedFloat = strtod(c_str(), &pEnd);
return *pEnd == 0;
}

};

Expand Down
10 changes: 3 additions & 7 deletions Foundation/library/includes/TTSymbol.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,10 @@ class TTFOUNDATION_EXPORT TTSymbol
mSymbolPointer = gTTSymbolTable.lookup(aString);
}


// TODO: make this a template for all numeric values
TTSymbol(const int int_to_convert_to_string)
{
TTString s;

s.append(int_to_convert_to_string);
TTString s = std::to_string(int_to_convert_to_string);
mSymbolPointer = gTTSymbolTable.lookup(s);
}

Expand Down Expand Up @@ -154,9 +152,7 @@ class TTFOUNDATION_EXPORT TTSymbol
/** Generate a pseudo-random symbol */
static TTSymbol random()
{
TTString s;

s.random();
TTString s = TTGenerateRandomString();
return TTSymbol(s);
}

Expand Down
9 changes: 3 additions & 6 deletions Foundation/library/includes/TTValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -390,18 +390,15 @@ class TTValue : public TTElementVector {
for (unsigned int i = 0; i < strList.size(); ++i) {
TTString currentString = strList.at(i).c_str();

if (currentString.toTTInt32(convertedInt) && !numberAsSymbol) {

if (TTStringToTTInt32(currentString, convertedInt) && !numberAsSymbol) {
at(n) = int(convertedInt);
n++;
}
else if (currentString.toTTUInt32(convertedUInt) && !numberAsSymbol) {

else if (TTStringToTTUInt32(currentString, convertedUInt) && !numberAsSymbol) {
at(n) = TTUInt32(convertedUInt);
n++;
}
else if (currentString.toTTFloat32(convertedFloat) && !numberAsSymbol) {

else if (TTStringToTTFloat32(currentString, convertedFloat) && !numberAsSymbol) {
at(n) = TTFloat64(convertedFloat); // cast float32 into float64
n++;
}
Expand Down
8 changes: 4 additions & 4 deletions Foundation/library/source/TTAddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ TTErr TTAddress::parseInstanceZero(const char* cstr, TTString& parsed)

parsed = toParse;

TTStringIter begin = parsed.begin();
TTStringIter end = parsed.end();
TTString::iterator begin = parsed.begin();
TTString::iterator end = parsed.end();

// parse and remove ".0"
while (!ttRegexForInstanceZero->parse(begin, end)) {
TTStringIter z_begin = ttRegexForInstanceZero->begin() - 2;
TTStringIter z_end = ttRegexForInstanceZero->end();
TTString::iterator z_begin = ttRegexForInstanceZero->begin() - 2;
TTString::iterator z_end = ttRegexForInstanceZero->end();

TTString a(begin, z_begin);
TTString b(z_end, end);
Expand Down
19 changes: 11 additions & 8 deletions Foundation/library/source/TTAddressBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ TTErr TTAddressBase::parse()
TTString s_instance;
TTString s_attribute;

TTStringIter begin = s_toParse.begin();
TTStringIter end = s_toParse.end();
TTString::iterator begin = s_toParse.begin();
TTString::iterator end = s_toParse.end();

// warning : addresses with special regex characters (like [, {) that could be problematic
// TODO : rewrite the parsing without regex because we want to use those special characters !
Expand All @@ -252,8 +252,8 @@ TTErr TTAddressBase::parse()
// parse directory
if (!ttRegexForDirectory->parse(begin, end))
{
TTStringIter temp_begin = ttRegexForDirectory->begin();
TTStringIter temp_end = ttRegexForDirectory->end();
TTString::iterator temp_begin = ttRegexForDirectory->begin();
TTString::iterator temp_end = ttRegexForDirectory->end();

s_directory = TTString(temp_begin, temp_end);

Expand Down Expand Up @@ -285,8 +285,8 @@ TTErr TTAddressBase::parse()
// parse attribute
if (!ttRegexForAttribute->parse(begin, end))
{
TTStringIter temp_begin = ttRegexForAttribute->begin();
TTStringIter temp_end = ttRegexForAttribute->end();
TTString::iterator temp_begin = ttRegexForAttribute->begin();
TTString::iterator temp_end = ttRegexForAttribute->end();

s_attribute = TTString(temp_begin, end);
s_toParse = TTString(begin, temp_end-1); // -1 to remove ":"
Expand Down Expand Up @@ -316,7 +316,7 @@ TTErr TTAddressBase::parse()
else
s_parent += TTString(ttRegexForParent->begin(), ttRegexForParent->end());

s_toParse = TTString(ttRegexForParent->end()+1, end-1); // +1 to remove "/", -1 to remove a useless \0
s_toParse = TTString(ttRegexForParent->end()+1, end); // +1 to remove "/"

begin = s_toParse.begin();
end = s_toParse.end();
Expand All @@ -329,7 +329,10 @@ TTErr TTAddressBase::parse()
// parse instance
if (!ttRegexForInstance->parse(begin, end))
{
s_instance = TTString(ttRegexForInstance->end(), end-1); // -1 to remove a '\0' at the end
// s_instance = TTString(ttRegexForInstance->end(), end-1); // -1 to remove a '\0' at the end
// The above changed to the below when going from std::vector-based TTString to std::string-based TTString because of crashes in the unit test
// The std::vector version used a NULL termination char whereas the std::string version does not.
s_instance = TTString(ttRegexForInstance->end(), end);
s_toParse = TTString(begin, ttRegexForInstance->begin()-1); // -1 to remove "."

begin = s_toParse.begin();
Expand Down
2 changes: 1 addition & 1 deletion Foundation/library/source/TTNodeDirectory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,7 @@ TTBoolean testNodeUsingFilter(TTNodePtr n, TTPtr args)

TTRegex* aRegex;
TTString s_toParse;
TTStringIter begin, end;
TTString::iterator begin, end;

// get filter
aFilter = TTDictionaryBasePtr((TTPtr)v[0]);
Expand Down
8 changes: 4 additions & 4 deletions Foundation/library/source/TTRegex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
using namespace boost;
using namespace std;
typedef boost::regex TTExpression;
typedef boost::match_results <TTStringIter> TTRegexStringResult;
typedef boost::match_results <TTString::iterator> TTRegexStringResult;
#else
#include <regex>
using namespace std;
Expand Down Expand Up @@ -52,7 +52,7 @@ TTRegex::~TTRegex()
}

//TTErr TTRegex::parse(TTRegexStringPosition& begin, TTRegexStringPosition& end)
TTErr TTRegex::parse(TTStringIter& begin, TTStringIter& end)
TTErr TTRegex::parse(TTString::iterator& begin, TTString::iterator& end)
{
if (regex_search(begin, end, mRESULT, mEXPRESSION))
return kTTErrNone;
Expand All @@ -62,7 +62,7 @@ TTErr TTRegex::parse(TTStringIter& begin, TTStringIter& end)

/** Get where start the result */
//TTRegexStringPosition TTRegex::begin()
TTStringIter TTRegex::begin()
TTString::iterator TTRegex::begin()
{
#if BOOST_REGEX
return mRESULT[1].first;
Expand All @@ -73,7 +73,7 @@ TTStringIter TTRegex::begin()

/** Get where end the result */
//TTRegexStringPosition TTRegex::end()
TTStringIter TTRegex::end()
TTString::iterator TTRegex::end()
{
#if BOOST_REGEX
return mRESULT[1].second;
Expand Down
40 changes: 38 additions & 2 deletions Foundation/library/source/TTString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,49 @@
#include "MersenneTwister.h"


void TTString::random()
std::string TTGenerateRandomString()
{
MTRand twister;
unsigned int i = (unsigned int)twister.randInt();
char s[16];
TTString str;

snprintf(s, 16, "j%u", i);
s[15] = 0;
(*this) = s;
str = s;
return str;
}


// TODO: These conversions seem a bit misguided, but are being maintained momentarily for backward compatibility.
// Should be replaced with standard calls like std::stoi() and friends.

TTBoolean TTStringToTTInt32(const std::string s, TTInt32& convertedInt)
{
char * pEnd;

convertedInt = (TTInt32)strtol(s.c_str(), &pEnd, 10);
return *pEnd == 0;
}


TTBoolean TTStringToTTUInt32(const std::string s,TTUInt32& convertedUInt)
{
char * pEnd;

convertedUInt = (TTUInt32)strtoul(s.c_str(), &pEnd, 10);

// is the last letter is a 'u' ?
return *pEnd == 'u' && pEnd == (s.c_str() + s.length() - 1);
}

/* note : isTTFloat32 works only because the TTInt32 case is matched before
see in TTValue::fromString method
*/
TTBoolean TTStringToTTFloat32(const std::string s, TTFloat32& convertedFloat)
{
char * pEnd;

convertedFloat = strtod(s.c_str(), &pEnd);
return *pEnd == 0;
}
Loading

0 comments on commit 5e85fd3

Please sign in to comment.