Skip to main content
gamedev.net
🔒 Locked

Write a string find and replace program

Started by nachilau Jun 27, 2007 at 4:10 PM 10 replies 1.4k views
Original Post
nachilau
nachilau
Hello, I am planning to write a program that will do string find and replace for any text file. Can anyone one give me any suggestion what is the best way to do it, or is there any existing library already provide this feature? I can write my own but it may not be efficient enough. Any suggestion? Thanks! Nacho
Nachi Lau (In Christ, I never die)www.sky-dio.com/Nachi
rip-off
rip-off
Regular expressions? Look into how the unix utilities like grep and sed work.
nachilau
nachilau
Not really a regular expression. Basically, what I want to do is do some pre-processing for my source code. However, since the program will pre-process a large amount of the source files, I want to find a efficient way to do it..

Nachi
Nachi Lau (In Christ, I never die)www.sky-dio.com/Nachi
Dancin_Fool
Dancin_Fool
What editor are you using for code, most editor's should have a find and replace all option.
nachilau
nachilau
Oh, what I want to do is to write a command line program so that I can do some pre-processing for my source code before it get compiled. So, I cannot do it in my IDE manually.

Nacho
Nachi Lau (In Christ, I never die)www.sky-dio.com/Nachi
Palidine
Palidine
Describe in more detail what you are trying to do. I can't see how regular expressions wouldn't be useful with string finding & replacing

-me
nachilau
nachilau
Ok, I don't say that the regular expression is not useful, what I mean is I think what I want don't need to be that complicated. Actually what I want to do is that, in the code,

int i = 10;
Hash hash = HASHINIT("Hello", 0x00000);

I want to write a program that parse my source code and then covert the my source code to

int i = 10;
Hash hash = HASHINIT("Hello", 0xae101010);

which assume 0xae101010 is the hash value of "Hello". So what I need to do is I need to search the string HASHINIT in the source code and then do some calculation and update the hash value. I hope it makes the question clear.

Nacho





Nachi Lau (In Christ, I never die)www.sky-dio.com/Nachi
Nathan Baum
Nathan Baum
Using Perl:

sub hash ($){    return "0xDEADBEEF"}while (<>) {    s/HASHINIT\("([^"]+)",\s+0x[0-9a-f-A-F]+\)/"HASHINIT(\"$1\", " . hash($1) . ")"/ge;    print;}


Fixing the hash value in your source code seems a bit suspect, though.
Palidine
Palidine
This seems like something much better done by (in order of preference):

1) a good architecture (whatever you are doing now is super bad if it requires code patching)
2) macros so the pre-processor can patch things up for you

-me
Zahlman
Zahlman
Any particular reason you can't just calculate the hash value in the Hash constructor?
nachilau
nachilau
Oh, the reason why I do this is that I don't want to calculate and hash value in the runtime. Also, when a programmer create a new hash value, what they only need to do is copy and past the existing MACRO line and modified the string, then the preprocessor will automatically the associate the hash value of that string accordingly.

Nacho
Nachi Lau (In Christ, I never die)www.sky-dio.com/Nachi
Palidine
Palidine
Quote:
Original post by nachilau
Oh, the reason why I do this is that I don't want to calculate and hash value in the runtime.


Why? Have you already implemented it this way and found (through the use of a profiler) that it's a performance problem?

An alternate (though horrible) solution that would be much better than what you're proposing (though infinitely better) would be to just put the strings in a seperate header file. When a programmer adds a new string, they add it to the header file and just increment the hash number. So the first string in the file would be 0, the next 1, etc.

But still, go with the constructor option until you find it's actually a performance issue.

-me

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.