I have a file, which I read into a buffer.
I wish to be able to replace a certain string in this buffer, which may appear on multiple lines.
I have a function which replaces a string within a string, and was wondering if there exists a similar function for the buffer
For example: the file contains the following:
2955 PASS
2956 REWORK: last status was:PASS
I wish to be able to replace
2956 REWORK: last status was:PASS
and end up with
2956 PASS (Replacing REWORK: last status was: with a blank
The file consists of many REWORK lines, and exceeds 255 characters.
When I attempt to use stringOf(buffer), I get a 254 character restriction.
My original thought was to put the file into a buffer, turn it into a string, modify the string, then convert the string back into the buffer.
Any suggestions?
Thank you
Gedinfo - Thu Nov 21 12:08:10 EST 2013 |
|
Re: How do I replace text in a buffer? GregM_dxler - Thu Nov 21 14:44:27 EST 2013
Hi,
There is no replace string in a buffer that I know of, but you could make one fairly easy using the contains or search commands and then extract everything except the string from the buffer (buffer = buffer[0:start of string -1] buffer[start of string + string length: ] ). Probably would need to have this in a loop to catch all instances of the string. Make sure you keep track of the index, as it will change when you extract the first string match.
Hope this helps,
Greg
|
|
Re: How do I replace text in a buffer? pommCannelle - Fri Nov 22 09:28:28 EST 2013
You can try this function ...
Buffer replace ( Buffer b, string toReplace, string newVal ){
Buffer rez = create
int pivot = 0
int iposition = contains ( b, toReplace, pivot )
while ( iposition != -1 ) {
rez += b[pivot:iposition-1]
rez += newVal
pivot = iposition + length toReplace
iposition = contains ( b, toReplace, pivot )
}
rez += b[pivot:]
return rez
}
Buffer b = create
b += "text to replace is here"
Buffer newBuff = replace( b, "is", "was" )
print tempStringOf newBuff
delete b
delete newBuff
Of course you have to add all the verifications needed ( to check if arguments are not null for example ... ). But i hope this code helps you to start ;)
Post your code if you are in trouble !
|
|
Re: How do I replace text in a buffer? Gedinfo - Fri Nov 22 12:54:39 EST 2013
Thank you very much for your suggestions.
|
|
Re: How do I replace text in a buffer? Mathias Mamsch - Sat Nov 23 15:59:48 EST 2013 pommCannelle - Fri Nov 22 09:28:28 EST 2013
You can try this function ...
Buffer replace ( Buffer b, string toReplace, string newVal ){
Buffer rez = create
int pivot = 0
int iposition = contains ( b, toReplace, pivot )
while ( iposition != -1 ) {
rez += b[pivot:iposition-1]
rez += newVal
pivot = iposition + length toReplace
iposition = contains ( b, toReplace, pivot )
}
rez += b[pivot:]
return rez
}
Buffer b = create
b += "text to replace is here"
Buffer newBuff = replace( b, "is", "was" )
print tempStringOf newBuff
delete b
delete newBuff
Of course you have to add all the verifications needed ( to check if arguments are not null for example ... ). But i hope this code helps you to start ;)
Post your code if you are in trouble !
Be aware of a memory leak in that code...
rez += b[pivot:iposition-1]
will leak a Buffer object every time you call it. The reason is, that doors defines the following perms:
|
Buffer ::+= (Buffer, Buffer)
|
|
Buffer ::+= (Buffer, string)
|
|
string ::[] (Buffer, Range_)
|
|
Buffer ::[] (Buffer, Range_)
|
which allow you to do:
string s = buf[0:10] // calls the string ::[] (Buffer, Range_) substring variant
Buffer b = buf[0:10] // calls the Buffer ::[] (Buffer, Range_) substring variant
Unfortunately if you do:
buf1 += buf2[0:10]
then this could mean both substring variants and DXL will choose the Buffer variant and therefore a temporary Buffer will be created for the substring, its contents appended to buf1, and then the temporary buffer will create a memory leak. Therefore you should always use combine(...) for appending a buffer to another one.
You can see the leakage, by executing the following code:
int *::+(int *ptr1, int ofs) { int *ptr2 = ptr1; ptr2+=ofs; return ptr2 }
int *::@(int *ptr, int ofs) { int ad = *(ptr + ofs); int *ptr2 = addr_ ad; return ptr2 }
int *getCurrentDXLContextPtr () {
DB x = create "";
int *ptr = addr_ x; int *result = ptr @ 48;
destroy x; return result;
}
int *getMemoryBlockNodes (int *cc) { return cc @ 0x74 }
int *nextNode (int *memNode) { return memNode @ 8 }
int countAllocatedObjects() {
int *memBlocks = getMemoryBlockNodes getCurrentDXLContextPtr()
int count = 0
while (!null memBlocks) { memBlocks = nextNode memBlocks; count++ }
return count
}
// comment me in to see the object counts increase;
print "Allocated Objects Before doing stuff: " countAllocatedObjects() "\n"
Buffer buf1 = create(), buf2 = create();
buf1 = "HelloLeak!";
print "After allocating buf1 & buf2: " countAllocatedObjects() "\n"
// now leak three objects, with the statment of that code
buf2 += buf1[5:8];
buf2 += buf1[5:8];
buf2 += buf1[5:8];
print "Allocated objects after Appening three times: " countAllocatedObjects() "\n"
print buf2 "!\n"
Sorry for going off topic, but that is a very common mistake and the code should not be copied like this. Regards, Mathias
|
|
Re: How do I replace text in a buffer? pommCannelle - Mon Nov 25 04:38:38 EST 2013 Mathias Mamsch - Sat Nov 23 15:59:48 EST 2013
Be aware of a memory leak in that code...
rez += b[pivot:iposition-1]
will leak a Buffer object every time you call it. The reason is, that doors defines the following perms:
|
Buffer ::+= (Buffer, Buffer)
|
|
Buffer ::+= (Buffer, string)
|
|
string ::[] (Buffer, Range_)
|
|
Buffer ::[] (Buffer, Range_)
|
which allow you to do:
string s = buf[0:10] // calls the string ::[] (Buffer, Range_) substring variant
Buffer b = buf[0:10] // calls the Buffer ::[] (Buffer, Range_) substring variant
Unfortunately if you do:
buf1 += buf2[0:10]
then this could mean both substring variants and DXL will choose the Buffer variant and therefore a temporary Buffer will be created for the substring, its contents appended to buf1, and then the temporary buffer will create a memory leak. Therefore you should always use combine(...) for appending a buffer to another one.
You can see the leakage, by executing the following code:
int *::+(int *ptr1, int ofs) { int *ptr2 = ptr1; ptr2+=ofs; return ptr2 }
int *::@(int *ptr, int ofs) { int ad = *(ptr + ofs); int *ptr2 = addr_ ad; return ptr2 }
int *getCurrentDXLContextPtr () {
DB x = create "";
int *ptr = addr_ x; int *result = ptr @ 48;
destroy x; return result;
}
int *getMemoryBlockNodes (int *cc) { return cc @ 0x74 }
int *nextNode (int *memNode) { return memNode @ 8 }
int countAllocatedObjects() {
int *memBlocks = getMemoryBlockNodes getCurrentDXLContextPtr()
int count = 0
while (!null memBlocks) { memBlocks = nextNode memBlocks; count++ }
return count
}
// comment me in to see the object counts increase;
print "Allocated Objects Before doing stuff: " countAllocatedObjects() "\n"
Buffer buf1 = create(), buf2 = create();
buf1 = "HelloLeak!";
print "After allocating buf1 & buf2: " countAllocatedObjects() "\n"
// now leak three objects, with the statment of that code
buf2 += buf1[5:8];
buf2 += buf1[5:8];
buf2 += buf1[5:8];
print "Allocated objects after Appening three times: " countAllocatedObjects() "\n"
print buf2 "!\n"
Sorry for going off topic, but that is a very common mistake and the code should not be copied like this. Regards, Mathias
thx for the tip Mathias ! :)
|
|
Re: How do I replace text in a buffer? pommCannelle - Mon Nov 25 04:53:31 EST 2013 Gedinfo - Fri Nov 22 12:54:39 EST 2013
Thank you very much for your suggestions.
( hereafter the corrected version, according the Mathias entry ;)
void combine ( Buffer b, string s ) {
Buffer tmp = create
tmp = s
combine ( b, tmp, 0 )
delete tmp
}
Buffer replace ( Buffer b, string toReplace, string newVal ){
Buffer rez = create
int pivot = 0
int iposition = contains ( b, toReplace, pivot )
while ( iposition != -1 ) {
combine ( rez, b, pivot, iposition-1 )
combine ( rez, newVal )
pivot = iposition + length toReplace
iposition = contains ( b, toReplace, pivot )
}
combine ( rez, b, pivot )
return rez
}
Buffer b = create
b = "text to replace is here"
Buffer newBuff = replace( b, "is", "was" )
print tempStringOf newBuff
delete b
delete newBuff
C u soon ! ;)
)
|
|