Computer Science Canada String Class: long strings plus manipulation functions |
Author: | DemonWasp [ Fri Feb 06, 2009 4:54 pm ] | ||
Post subject: | String Class: long strings plus manipulation functions | ||
It occurred to me that having a limit of 255 characters to a string (presumably because Holtsoft's implementation uses a char [256] in the background) is quite restrictive. Even worse, Turing strings have minimal methods associated with them. So, if your project happens to require a more powerful string implementation, here's one. It's based on the Java String API, but unlike Java strings, this one is mutable, which means you can change its contents (as can any function you pass it into). Documentation is pretty sparse, but most things are similar to the Java String API. I'd be happy to add more, but I'm off shortly. You can run my simple smoke-test application by running StringTest.t . To use String.t in your own programs, just have
at the top of your file; that tells Turing to pretend that the contents of String.t are right there at the top of the file. Suggestions, criticisms, comments all welcome. |
Author: | andrew. [ Fri Feb 06, 2009 7:37 pm ] |
Post subject: | RE:String Class: long strings plus manipulation functions |
Very nice!! It looks great. One problem though, I get an error when it gets to the stress test. It opens your String.t file and points to line 370. The error is I/O attempted on unopened stream number -10. Open on line 255........ |
Author: | DemonWasp [ Sat Feb 07, 2009 10:38 pm ] |
Post subject: | RE:String Class: long strings plus manipulation functions |
That's because I didn't include test.txt. Just put whatever you want in there; I took the UTF-8 article from Wikipedia, copy-pasted it into Notepad, then saved in ANSI format (it'll warn you about some characters not being available in ANSI, ignore it. Turing doesn't appear to handle anything other than ANSI). |
Author: | SNIPERDUDE [ Sun Feb 08, 2009 8:56 pm ] | ||||
Post subject: | Re: String Class: long strings plus manipulation functions | ||||
That is a very useful tool to have. Thank you for your share. I created a simple function about a year ago that mimics VB's Tokenize function. I've certainly found it useful. Here it is:
What this basically does is reads a sentence and returns the next word each time you call it. So if you wanted to read a sentence and call each word, it would look like this:
I have found this function quite useful for reading files for game information. You don't just have to use a space for the token either, could be anything. I do hope to expand it a bit to be more flexible like VB's, but as of the moment it is still efficient enough. It will keep drawing the next chunk of text from that sentence until it recognizes the sentence is different. |
Author: | DemonWasp [ Mon Feb 09, 2009 12:57 am ] |
Post subject: | RE:String Class: long strings plus manipulation functions |
Hmm...a good point, it may be worth implementing StringTokenizer (which is also available in the Java API) in Turing. Word of warning: although I made an effort to code efficiently, I make no promises as to the performance of this String class. Be aware that outputting from these strings may be quite slow (to the screen, anyway). Other operations should be quick, but will still run much faster on a Turing-native string of the same size (as those operations will be run in compiled methods within Turing, rather than my Turing code. |
Author: | DemonWasp [ Mon Feb 09, 2009 11:29 am ] | ||
Post subject: | Re: String Class: long strings plus manipulation functions | ||
Addition: created a StringTokenizer class, corrected some bugs in certain String methods not tested by StringTest.t. The new StringTokenizer has methods create(^String, ^String), createBasic (^String, string), hasMoreTokens(), nextToken(), countTokens(), all of which behave more-or-less as Java's specification. I haven't done any formal testing (again!) but here's an example:
Again, all comments / suggestions / criticisms are welcome. |
Author: | Tony [ Mon Feb 09, 2009 4:01 pm ] | ||
Post subject: | RE:String Class: long strings plus manipulation functions | ||
Quote: #Tokens = 1 Token #1 = 'SCII stands for American Standard Code for Information Interchange. Computers can only understand numbers, so an ASCII code is the numerical representation of a character such as 'a' or '@' or an action of some sort.' |
Author: | DemonWasp [ Mon Feb 09, 2009 4:45 pm ] |
Post subject: | Re: String Class: long strings plus manipulation functions |
Hmm...not exactly a standard use-case, tokenizing a string with a blank delimiter, but you make a good point. In fact, that error turned out to be indicative of a larger issue - I had a 1 where I should have had a delim->leng(). Fixed version now outputs the full String if you delimit with a blank string, and should delimit correctly even with multi-character delimiters. |
Author: | Tony [ Mon Feb 09, 2009 5:12 pm ] |
Post subject: | RE:String Class: long strings plus manipulation functions |
A blank delimiter is used to split a string into individual characters, so I would consider it a valid use (the popularity of which though, is up to one's interpretation). |
Author: | DemonWasp [ Mon Feb 09, 2009 5:56 pm ] |
Post subject: | RE:String Class: long strings plus manipulation functions |
Hmmm...I'm prone to not implementing that, since you could just use charAt() to achieve the same effect (indexing characters as an array). |