🔒 Locked
Write a string find and replace program
Advertisement
Original Post
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
What editor are you using for code, most editor's should have a find and replace all option.
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
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
Using Perl:
Fixing the hash value in your source code seems a bit suspect, though.
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.
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
Nacho
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.
Advertisement