diff options
| author | Mike Buland <eichlan@xagasoft.com> | 2007-11-23 23:47:37 +0000 |
|---|---|---|
| committer | Mike Buland <eichlan@xagasoft.com> | 2007-11-23 23:47:37 +0000 |
| commit | c9574d3f77081fb4a654d42c298d6ebf34abca51 (patch) | |
| tree | 0410f4d63923984eb3fe2daac10bae9145e45032 | |
| parent | cd215f0da23e16c3f1a7200f2b9f67f23c9b4be7 (diff) | |
| download | libbu++-c9574d3f77081fb4a654d42c298d6ebf34abca51.tar.gz libbu++-c9574d3f77081fb4a654d42c298d6ebf34abca51.tar.bz2 libbu++-c9574d3f77081fb4a654d42c298d6ebf34abca51.tar.xz libbu++-c9574d3f77081fb4a654d42c298d6ebf34abca51.zip | |
Bu::FString now has insert and remove functions, yay!
Diffstat (limited to '')
| -rw-r--r-- | build.conf | 1 | ||||
| -rw-r--r-- | src/fstring.h | 39 | ||||
| -rw-r--r-- | src/unit/fstring.cpp | 30 |
3 files changed, 70 insertions, 0 deletions
| @@ -52,6 +52,7 @@ filesIn("src/unit") filter regexp("^src/unit/(.*)\\.cpp$", "unit/{re:1}"): | |||
| 52 | rule "exe", | 52 | rule "exe", |
| 53 | target file, | 53 | target file, |
| 54 | group "tests", | 54 | group "tests", |
| 55 | group "unit", | ||
| 55 | requires "libbu++.a", | 56 | requires "libbu++.a", |
| 56 | set "CXXFLAGS" += "-Isrc", | 57 | set "CXXFLAGS" += "-Isrc", |
| 57 | set "LDFLAGS" += "-L. -lbu++", | 58 | set "LDFLAGS" += "-L. -lbu++", |
diff --git a/src/fstring.h b/src/fstring.h index 8cccd5c..8b512e2 100644 --- a/src/fstring.h +++ b/src/fstring.h | |||
| @@ -226,6 +226,45 @@ namespace Bu | |||
| 226 | prependChunk( pNew ); | 226 | prependChunk( pNew ); |
| 227 | } | 227 | } |
| 228 | 228 | ||
| 229 | void insert( long nPos, const chr *pData, long nLen ) | ||
| 230 | { | ||
| 231 | if( nLen <= 0 ) | ||
| 232 | return; | ||
| 233 | flatten(); | ||
| 234 | if( nPos <= 0 ) | ||
| 235 | { | ||
| 236 | prepend( pData, nLen ); | ||
| 237 | } | ||
| 238 | else if( nPos >= nLength ) | ||
| 239 | { | ||
| 240 | append( pData, nLen ); | ||
| 241 | } | ||
| 242 | else | ||
| 243 | { | ||
| 244 | Chunk *p1 = newChunk( nPos ); | ||
| 245 | Chunk *p2 = newChunk( nLen ); | ||
| 246 | Chunk *p3 = newChunk( nLength-nPos ); | ||
| 247 | cpy( p1->pData, pFirst->pData, nPos ); | ||
| 248 | cpy( p2->pData, pData, nLen ); | ||
| 249 | cpy( p3->pData, pFirst->pData+nPos, nLength-nPos ); | ||
| 250 | clear(); | ||
| 251 | appendChunk( p1 ); | ||
| 252 | appendChunk( p2 ); | ||
| 253 | appendChunk( p3 ); | ||
| 254 | } | ||
| 255 | } | ||
| 256 | |||
| 257 | void remove( long nPos, long nLen ) | ||
| 258 | { | ||
| 259 | if( nLen <= 0 || nPos < 0 || nPos >= nLength ) | ||
| 260 | return; | ||
| 261 | if( nLen >= nLength-nPos ) | ||
| 262 | nLen = nLength-nPos-1; | ||
| 263 | flatten(); | ||
| 264 | cpy( pFirst->pData+nPos, pFirst->pData+nPos+nLen, nLen+1 ); | ||
| 265 | nLength -= nPos; | ||
| 266 | } | ||
| 267 | |||
| 229 | /** | 268 | /** |
| 230 | *@todo void prepend( const chr &cData ) | 269 | *@todo void prepend( const chr &cData ) |
| 231 | */ | 270 | */ |
diff --git a/src/unit/fstring.cpp b/src/unit/fstring.cpp index 69c7e0a..03df9c3 100644 --- a/src/unit/fstring.cpp +++ b/src/unit/fstring.cpp | |||
| @@ -18,6 +18,8 @@ public: | |||
| 18 | addTest( Unit::compare2 ); | 18 | addTest( Unit::compare2 ); |
| 19 | addTest( Unit::appendSingle ); | 19 | addTest( Unit::appendSingle ); |
| 20 | addTest( Unit::shared1 ); | 20 | addTest( Unit::shared1 ); |
| 21 | addTest( Unit::insert ); | ||
| 22 | addTest( Unit::remove ); | ||
| 21 | } | 23 | } |
| 22 | 24 | ||
| 23 | virtual ~Unit() | 25 | virtual ~Unit() |
| @@ -57,6 +59,34 @@ public: | |||
| 57 | a = b; | 59 | a = b; |
| 58 | unitTest( a.getStr() == b.getStr() ); | 60 | unitTest( a.getStr() == b.getStr() ); |
| 59 | } | 61 | } |
| 62 | |||
| 63 | void insert() | ||
| 64 | { | ||
| 65 | Bu::FString a("abcd"); | ||
| 66 | a.insert( 2, "-!-", 3 ); | ||
| 67 | unitTest( a == "ab-!-cd" ); | ||
| 68 | |||
| 69 | a.insert( 0, "!!", 2 ); | ||
| 70 | unitTest( a == "!!ab-!-cd" ); | ||
| 71 | |||
| 72 | a.insert( -10, "789", 3 ); | ||
| 73 | unitTest( a == "789!!ab-!-cd" ); | ||
| 74 | |||
| 75 | a.insert( 12, "89", 2 ); | ||
| 76 | unitTest( a == "789!!ab-!-cd89" ); | ||
| 77 | |||
| 78 | a.insert( 1203, "12", 2 ); | ||
| 79 | unitTest( a == "789!!ab-!-cd8912" ); | ||
| 80 | } | ||
| 81 | |||
| 82 | void remove() | ||
| 83 | { | ||
| 84 | Bu::FString a("abHEYcd"); | ||
| 85 | a.remove( 2, 3 ); | ||
| 86 | unitTest( a == "abcd" ); | ||
| 87 | a.remove( 2, 5 ); | ||
| 88 | unitTest( a == "ab" ); | ||
| 89 | } | ||
| 60 | }; | 90 | }; |
| 61 | 91 | ||
| 62 | int main( int argc, char *argv[] ) | 92 | int main( int argc, char *argv[] ) |
