EC Strings Design

From EcereWiki
Jump to: navigation, search
   [   ] String dataType
   [   ] Solution for properties and text format strings


[   ] Strings
   char * name;
   name.ext:
      char tempString1[MAX_EXTENSION];
      (String_GetExtension(name, tempString1), tempString1)

   if(name.ext == "this") :
      char tempString1[MAX_EXTENSION];
      if(Operator_String_Equal((String_GetExtension(name, tempString1), tempString1), "this"))

      (Return type of String_GetExtension is char[MAX_EXTENSION])

   char * yo = "yo";
   char * bla;
   bla = "This is " + yo + ", ok?";
   delete bla;

      * Type of Operator_String_Add is "unassigned string"
   bla = Operator_String_Add_UnassignedString_String(Operator_String_Add_String_String("This is ", yo), ", ok?");
   if(bla) free(bla);

   char * bla = "This" + " is";
   bla += " nice";
   delete bla;

      - Gets len of "This", gets len of " is"
      - Allocates enough space
      - Copies string1, strcat string2
      - Gets len of " nice"
      - reallocate, strcat
      - frees

   // Optional!
   char * bla = "This";
   bla += " nice";
   delete bla;

   // Correct:
   char * bla { "This" };
   bla += " nice";
   delete bla;

   char hey[100];
   char * yo = "hey" + " you";
   hey = yo;
   delete yo;

   // Just copies the address:
   char hey[100] = "hey you";
   char * yo = hey;

   char * a = "this";
   char hey[100] = "hey " + a + "you";

      make a string out of "hey " + a
      reallocates, strcat "you"
      strcpy to hey, frees it
   [   ] Verify string reference / string copy


Sacrebleu's String functions and commentary:

// This is probably the place where Garbage collection is most useful, otherwise it should be up to the user for Classes (I've always thought)
// It's also the place where you don't see the inefficiency of garbage collection and can bog it down.. something to consider

// "b" == 'b'!

// Users should be able to add default String class functions.. by redefining the class with new functions or replacements

// %= should mean "like" (case insensitive comparison).. or you could do this:  if ( a like b ) ?

Char c,d;
String s=s.load("filename.txt");
String y=y.loadRTF( "raw.rtf" ); // strips tags?
StringList sList=s.load("filename"); // (one line per entry)
StringList xList; // be consistent with 0..n or 1..n for this and the .words()
char *a;

c=s.char(1); // or chr?
d=c.upper(); 
c=c.lower();

if ( s.isEmpty() ) {}

s=s+s;

sList.merge(xList); // merges sList with xList storing in sList
sList.interlace(xList,sList); // merges interlaced starting with xList[0], then sList[1] .. into sList
sList.unique(); // removes non-unique entries from sList


delete s;
s= sList.toString(); // (concatenates a string list and returns it as a String)
s.trim(); // trims spaces from ends
s.unpad(); // removes duplicate spaces throughout
s.pluralize(); // changes string s from 'sky' to 'skies'
s.trimR();
s.trimL();
foreach ( s in sList ) {
 s.filter("b"); // removes letter b from string s which is a single element of list sList
 if ( s.inList() == 2 ) // it's the second element in the sList
}
s.lower();
s.upper();
s.filter("012345789K"); // removes any character on list

delete sList;
sList = s.split("\n");

sList.alphabetize( true ); // ascending
sList.alphabetize( false ); // descending
xList=sList.set( "substring" ); // returns a subset matching case-sensitive substring
if ( sList.contains( "some phrase" ) ) 
s=sList.paragraph(); // trims, removes \ns 
xList=sList.wordwrap();  // word-wraps and indents to create multiple paragraphs, returns each paragraph as a string
delete s;
s=xList.implode("\n"); // returns a bunch of strings in xList with \n in between
delete s;
delete xList;

if ( s.word(1) == "butter" ) {}
if ( s.wordLower(1) == "butter" ) {} ?
xList = s.words();  // returns each argument (detects "quoted" or 'quoted' as one word) as elements of xList 

if ( s.isNumeric() && s.isFloat() ) { ... }
if ( s.isInt() ) { ... }
if ( s.isPrefix( "command" ) ) { ... } returns if string is a prefix of "command" 
if ( s.hasString( y ) ) {...;}
if ( s.equals( "fish" ) ) {...}
if ( s.means( "FiSh" ) ) // matches all variations of fish and FiSH and FISH and .. equalsCaseless() instead?

for ( int cc=s.countChars( "b" ); cc>0; cc-- ) { d=s.chr(6); delete d; }

s.hash( 200 ); // replaces content with MD5 hash of length 200

xList.deleteAll();
xList.deleteMatching( "rubber band" ); // deletes all elements matching precisely (case insensitive)
xList.deleteMatchingCase( "fISh" ); // deletes all matching precisely (case sensitive)
xList.deleteLike( "foo" ); // deletes any element that contains foo (case insensitive)
xList.deleteLikeCase( "Foo" ); // case sensitive version of above

xList.replace( "foo", "bar" );
xList.replaceCase( "Foo", "Bar" );
xList.replaceAll( "Foo", "bar" ); // would replace 'Foo' or 'foo' with 'bar'
xList.replaceAllCase( "foO", "baR" ); 
xList.replaceMatchCase( "foo", "bar" ); // would replace 'Foo' with 'Bar' and 'foo' with 'bar'
xList.replaceAllMatchCase( "foo", "bar" ); // would replace 'Foo' with 'Bar' and 'foo' with 'bar'

s.replace( "foo", "bar" );
s.replaceCase( "Foo", "Bar" );
s.replaceAll( "Foo", "bar" );
s.replaceAllCase( "foO", "baR" );
xList.replaceMatchCase( "foo", "bar" ); // would replace 'Foo' with 'Bar' and 'foo' with 'bar'
xList.replaceAllMatchCase( "foo", "bar" ); // would replace 'Foo' with 'Bar' and 'foo' with 'bar'
xList.push( "some string" );
String v=xList.pop();
delete xList;

xList=s.spellcheck(); // returns a list of all mispelled words as they originally appear, using ispell?
delete xList;
xList=sList.spellcheck(); // for a list
foreach( x in xlist ) ..

delete s;
s=xList.string(5); // returns the 5th string, or "" if its beyond the length
xList.delete(5); // deletes element 5 from the list if it exists
delete s;
xList.insert(4,"string"); // inserts a string at element 4
s.insert(10,"some stuff"); // inserts some stuff at character 10
s.insertAfter(5,"a new phrase" ); // inserts "a new phrase" after word 5.. example: fe fi fo fum i smell, would yield "fe fi fo fum i a new phrase smell"

int y=s.linecount();
int z=s.wordcount();
y=s.charCount();

z=sList.linecount();
y=sList.wordcount();
String k=sList.word(22); // returns word 22 starting from element 1
delete k;
k=sList.string(5).word(4) // wouldn't this be nice..
delete k;
xList=sList.words(); // returns each word of each element in an ordered list

s.write( "somefile.txt" ); // or s.dump()? 
s.appendFile( "somefile.txt" ); // appends to end
s.append( "some stuff" );
s.prepend( "some stuff" );

xList.addIfUnique( s );

delete s,c,d;
delete sList, xList;

The SuperString class:

class SuperString : Array<char>
{
   property char *
   {
      get { return array; }
      set { SuperString s = SuperString { size = strlen(value)+1 }; memcpy(s.array, value, s.size); return s; }
   }

   property String c_str
   {
      get { return array; }
   }

   property int count { get { return size-1; } }

   void assign(char * s, int len)
   {
      size = len + 1;
      memcpy(array, s, len);
      ((char *)array)[len] = 0;
   }

   char * OnGetString(char * tempString, void * fieldData, bool * needClass)
   {
      return array;
   }
};