Created
October 8, 2014 18:15
-
-
Save jerstlouis/314b265851200e7a4568 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import "ecere" | |
| // TODO: Will want it to be on the stack but be ref counted and have constructors, destructors | |
| public enum StringAllocType { pointer, stack, heap }; | |
| public struct ZString | |
| { | |
| private: | |
| char * _string; | |
| int len; | |
| StringAllocType allocType; | |
| int size; | |
| /*int minSize; | |
| int maxSize; | |
| ZString() | |
| { | |
| maxSize = MAXINT; | |
| }*/ | |
| ~ZString() | |
| { | |
| if(allocType == heap) | |
| delete _string; | |
| } | |
| void copyString(char * value, int newLen) | |
| { | |
| if(allocType == pointer) | |
| { | |
| size = 0; | |
| _string = null; | |
| allocType = heap; | |
| } | |
| if(allocType == heap) | |
| { | |
| int newSize = newLen ? newLen + 1 : 0; | |
| if(newSize != size) | |
| { | |
| /*if(newSize < minSize) newSize = minSize; | |
| else if(newSize > maxSize) newSize = maxSize;*/ | |
| if(newSize && size) | |
| _string = renew _string char[newSize]; | |
| else if(newSize) | |
| _string = new char[newSize]; | |
| else | |
| delete _string; | |
| size = newSize; | |
| } | |
| } | |
| if(newLen + 1 > size) newLen = size-1; | |
| len = newLen; | |
| if(value) | |
| { | |
| memcpy(_string, value, newLen); | |
| _string[newLen] = 0; | |
| } | |
| } | |
| public: | |
| char * OnGetString(char * tempString, void * fieldData, bool * needClass) | |
| { | |
| return _string; | |
| } | |
| bool OnGetDataFromString(char * string) | |
| { | |
| property::string = string; | |
| return true; | |
| } | |
| property char * string | |
| { | |
| set { copyString(value, value ? strlen(value) : 0); } | |
| get { return _string; } | |
| } | |
| property char * | |
| { | |
| get { return _string; } | |
| set | |
| { | |
| len = value ? strlen(value) : 0; | |
| _string = value; | |
| allocType = pointer; | |
| } | |
| } | |
| void concat(ZString s) | |
| { | |
| if(s && allocType != pointer) | |
| { | |
| int addedLen = s.len; | |
| int newLen = len + addedLen; | |
| if(allocType == heap && newLen + 1 > size) | |
| { | |
| int newSize = newLen + 1; | |
| /*if(newSize > maxSize) | |
| newSize = maxSize;*/ | |
| if(newSize > size) | |
| { | |
| _string = renew _string char[newLen]; | |
| size = newSize; | |
| } | |
| } | |
| if(newLen + 1 > size) | |
| addedLen = size - 1 - len; | |
| if(addedLen > 0) | |
| { | |
| memcpy(_string + len, s._string, addedLen); | |
| len += addedLen; | |
| _string[len] = 0; | |
| } | |
| } | |
| } | |
| void copy(ZString s) | |
| { | |
| copyString(s._string, s.len); | |
| } | |
| void free() | |
| { | |
| if(allocType == heap) | |
| delete _string; | |
| } | |
| }; | |
| class StringTest : Window | |
| { | |
| caption = $"Form1"; | |
| background = formColor; | |
| borderStyle = sizable; | |
| hasMaximize = true; | |
| hasMinimize = true; | |
| hasClose = true; | |
| clientSize = { 632, 438 }; | |
| Button button1 | |
| { | |
| this, caption = $"button1", position = { 128, 152 }; | |
| bool NotifyClicked(Button button, int x, int y, Modifiers mods) | |
| { | |
| ZString foo = "Hello, "; // No destructor will be needed for this once made a struct | |
| ZString bar { "eC Strings!" }; | |
| ZString fooBar { foo }; | |
| Point a { 1, 2 }; | |
| //ZString<30> buf = "Hello, "; | |
| char charBuf[30]; | |
| char * s; | |
| ZString buf { allocType = stack, size = sizeof(charBuf), _string = charBuf }; | |
| buf.string = "Testing a 30 stack string"; | |
| buf.concat(": Adding stuff that will go past the stack!"); | |
| { | |
| //PrintLn("Hello" + "World"); | |
| ZString a { "Hello" }; | |
| a.concat(" World"); | |
| PrintLn(a); | |
| a.free(); | |
| } | |
| //delete a; | |
| // ZString foo = "This" + " is" + " a " + " test"; | |
| //int [100] array = [ 1, 2, 3 ]; // Array<int> { minAllocSize = 100 } | |
| //int [] array = [ 1, 2, 3 ]; // Array<int> { } | |
| fooBar.concat(bar); | |
| PrintLn(fooBar); | |
| PrintLn(buf); | |
| foo.free(); | |
| bar.free(); | |
| fooBar.free(); | |
| buf.free(); | |
| return true; | |
| } | |
| }; | |
| } | |
| StringTest form {}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment