vendredi 27 février 2015

String searching in Rebol or Red


Vote count:

0




While this is a real question, and one which I think applies to red as well as r2, I will confess I wouldn't mind 20 points, either.


I'm interested in search/replace operations on strings, with a mind to try to hack out a sed-like utility in rebol as a learning exercise, at least. So I imagine the utility doing a lot of searching on a lot of long strings. As a baby step/learning experience I decided to search for a character:




pos: index? find STR "z" == 18 pos == 18 Impressive! Rebol is so powerful!:-) Let's search for something else...





>> pos: index? find STR "n"
** Script Error: index? expected series argument of type: series port
** Where: halt-view
** Near: pos: index? find STR "n"


What? :-(




pos == 18 Yeah, there was no "n" in the string I was searching. But so what, what is the benefit of an interpreter blowing up instead of doing something sensible, such as returning a testable "null" char in pos? Or, I'm sure there are other civilized ways this could be handled.




I was told I should have done this:



>> if found? find STR "z" [pos: index? find STR "z"]
== 18

>> if found? find STR "n" [pos: index? find STR "n"]
== none
>> pos
== 18


Really? I have to search the string TWICE; the first time just to be sure it is "safe" to search AGAIN? Can you imagine the laughter if the original sed had been discovered to have been implemented this senselessly?


So I have a two part question, or maybe three: (1) How would a wizard implement my search function? I presume there is a wizardly better way better than this.... (2) Is red going to fix this ...ummm...er...ahhh..."feature"? Ideally I'd think find should return a valid string position or a NULL if it hits end of string (NULL delimited, may I presume?). The NULL is FALSE so that would set up for a really easy if test. (3) What is the most CPU effective way to do a replace once I have a valid index? There appear to so many choices in rebol (a good thing) that it is possible to get stuck in choosing or stuck in a suboptimal choice. (4) I wasn't going to actually ask this, as it sounds a bit snarky, but I'm honestly curious: how could such an obviously ...ummm....er...suboptimal implementation of find go decades without being addressed?


P.S. If I don't care I don't say anything... I care about rebol and, especially, red; I would like red to get it right out of the gate and I don't think requiring a programmer to search a string TWICE, or face an ugly death, is how we want to present to the world.



asked 52 secs ago







String searching in Rebol or Red

Aucun commentaire:

Enregistrer un commentaire