Hi All, |
Re: Remove newline ("\") from a string/buffer
About as simple as can be made:
Buffer b2=create
Buffer StripNewLine(Buffer b)
{ int j,l=length(b)
b2=""
for(j=0;j<l;j++){if(b[j]!='\n')b2+=b[j]}
return b2
}
Buffer b1 =create
b1= "First Line\nSecond Line"
print StripNewLine(b1) ""
|
Re: Remove newline ("\") from a string/buffer OurGuest - Fri Jul 15 15:22:03 EDT 2011
About as simple as can be made:
Buffer b2=create
Buffer StripNewLine(Buffer b)
{ int j,l=length(b)
b2=""
for(j=0;j<l;j++){if(b[j]!='\n')b2+=b[j]}
return b2
}
Buffer b1 =create
b1= "First Line\nSecond Line"
print StripNewLine(b1) ""
You have a weak structure here. The way you have it you must tell the calling program to use the buffer and abandon it before calling the function again, since b2 is external and created only once. If the caller deletes the buffer b1, then your function will fail.
void StripNewLine(Buffer &b)
{ Buffer b2=create
int j,l=length(b)
if (l == 0) return // nothing to do
b2=""
for(j=0;j<l;j++){if(b[j]!='\n')b2+=b[j]}
delete(b) // now 'swap' the buffers
b = b2 // this is 'alias', not 'copy'
}
Buffer b1 =create
b1= "First Line\nSecond Line"
StripNewLine(b1)
print tempStringOf(b1) ""
delete(b1)
|
Re: Remove newline ("\") from a string/buffer llandale - Sat Jul 16 00:51:03 EDT 2011
You have a weak structure here. The way you have it you must tell the calling program to use the buffer and abandon it before calling the function again, since b2 is external and created only once. If the caller deletes the buffer b1, then your function will fail.
void StripNewLine(Buffer &b)
{ Buffer b2=create
int j,l=length(b)
if (l == 0) return // nothing to do
b2=""
for(j=0;j<l;j++){if(b[j]!='\n')b2+=b[j]}
delete(b) // now 'swap' the buffers
b = b2 // this is 'alias', not 'copy'
}
Buffer b1 =create
b1= "First Line\nSecond Line"
StripNewLine(b1)
print tempStringOf(b1) ""
delete(b1)
Don't exchange the buffer by a new one (invalidating all 'aliases' or copies of the buffer handle). Better use a temporary Buffer then copy the stuff to the old buffer. void StripNewLine(Buffer b) { Buffer b2=create int j,l=length(b) if (l == 0) return // nothing to do b2="" for(j=0;j<l;j++){if(b[j]!='\n')b2+=b[j]} delete(b) // now 'swap' the buffers b = b2 // this is 'alias', not 'copy' } Buffer b1 =create b1= "First Line\nSecond Line" StripNewLine(b1) print tempStringOf(b1) "" delete(b1) Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS |
Re: Remove newline ("\") from a string/buffer llandale - Sat Jul 16 00:51:03 EDT 2011
You have a weak structure here. The way you have it you must tell the calling program to use the buffer and abandon it before calling the function again, since b2 is external and created only once. If the caller deletes the buffer b1, then your function will fail.
void StripNewLine(Buffer &b)
{ Buffer b2=create
int j,l=length(b)
if (l == 0) return // nothing to do
b2=""
for(j=0;j<l;j++){if(b[j]!='\n')b2+=b[j]}
delete(b) // now 'swap' the buffers
b = b2 // this is 'alias', not 'copy'
}
Buffer b1 =create
b1= "First Line\nSecond Line"
StripNewLine(b1)
print tempStringOf(b1) ""
delete(b1)
Nit Pick:
Buffer bTempStrip = create() // create the temp buffer outside of the function for performance
void StripNewLine(Buffer b)
{
setempty bTempStrip ; // empty the temp buffer
int j,l=length(b)
if (l == 0) return // nothing to do
b2 = ""
for(j=0;j<l;j++) { if(b[j] != '\n') bTempStrip += b[j] }
setempty b; combine (b, bTempStrip, 0) // empty the old buffer, and replace its contents
}
Buffer b1 =create
Buffer b1Alias = b1
b1= "First Line\nSecond Line"
StripNewLine(b1)
print (tempStringOf b1) ""
print "Hey our alias still works: " (tempStringOf b1Alias) ""
delete(b1)
Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS
|
Re: Remove newline ("\") from a string/buffer Mathias Mamsch - Sat Jul 16 08:45:35 EDT 2011 Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS |
Re: Remove newline ("\") from a string/buffer Mathias Mamsch - Sat Jul 16 08:50:53 EDT 2011
Nit Pick:
Buffer bTempStrip = create() // create the temp buffer outside of the function for performance
void StripNewLine(Buffer b)
{
setempty bTempStrip ; // empty the temp buffer
int j,l=length(b)
if (l == 0) return // nothing to do
b2 = ""
for(j=0;j<l;j++) { if(b[j] != '\n') bTempStrip += b[j] }
setempty b; combine (b, bTempStrip, 0) // empty the old buffer, and replace its contents
}
Buffer b1 =create
Buffer b1Alias = b1
b1= "First Line\nSecond Line"
StripNewLine(b1)
print (tempStringOf b1) ""
print "Hey our alias still works: " (tempStringOf b1Alias) ""
delete(b1)
Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS
Dang, hadn't thought of previous Aliases because I don't do that. Got several old and tried library functions that do this, had to recode them all. And so I did timing tests: doing 100,000 loops through fReplaceChar(), I see that... When buffer is length 35 the Alias method takes 1515ms and the new takes 1422ms (little difference) When buffer is length 4480 the Alias method takes 23297ms and the new takes 12844ms (big difference)
|
Re: Remove newline ("\") from a string/buffer llandale - Sat Jul 16 15:41:53 EDT 2011
Dang, hadn't thought of previous Aliases because I don't do that. Got several old and tried library functions that do this, had to recode them all. And so I did timing tests: doing 100,000 loops through fReplaceChar(), I see that... When buffer is length 35 the Alias method takes 1515ms and the new takes 1422ms (little difference) When buffer is length 4480 the Alias method takes 23297ms and the new takes 12844ms (big difference)
Buffers have a capacity and a length. The capacity says how much the buffer can take until, it will need to reallocate the memory. Like an array, the buffer capacity will grow by a certain factor (3/2) or something. The timing difference probably results from the reallocation of the buffer, as soon as it hits a certain length. By default the buffers have a capacity of 120 or something like this. This means for a 4000 character string, it will grow 5-8 times before it reaches its final length. For each growing DOORS will deallocate the old memory, allocate a bigger block and copy stuff over. That is probably what makes up the timing difference. You can try allocating the buffer with: Buffer b = create(5000) // allocate with a capacity of 5000
Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS
|
Re: Remove newline ("\") from a string/buffer I appreciate all of your input and sample code. However, is this the easiest way to do this? It is too bad that DXL did not have some sort of Trim function or something. I also wish that the DXL ref manual had much better examples and instruction. I have been programming in other languages for years, and I have never seen more worse examples or lack of instruction than in the ref. manual. most of it is so vague it is hard to tell what is what. And then they use an example of like Ex. User user . WHY??!! Somwthing like Ex. User vUsr would be MUCH better! Then I know that vUsr is a variable, or I could have done bUsr, guess that could be a buffer. Is there any online, or book, reference manuals or tutorials that can help out. And then searching in the forums is a challenge too. If I want to find something specific I have to look through a bunch of stuff. Sometimes the more complex my search terms are then the more crap I get back that is useless. Ok, I better get off my soapbox, I don't want to piss anybody off. :) Thanks for the help. Jerry PS. if you have a better way to remove characters, or the newline(\n), etc, then post or email me. I am still looking for something shorter, eaiser, quicker, etc. :) |
Re: Remove newline ("\") from a string/buffer Oh, I forgot to ask about the dreaded Regexp/Regexp2. It is in the dreaded reference manual, but again, kind of cryptic. Would this be a good way to remove the newline(\n)? If so then a nice piece of sample code, showing top down, "proper" structure, would be GREATLY appreciated. The ref manual says to use Regexp2 - instead of Regexp - but does not really show a good example. What do you guys think? How can I use the ".*" to strip a string from any non string characters? Some of the fields, like Description, might have newlines in them. Right now I am using the buffer to get the information from the userlist through a for loop. I parse the data into a string. So, vDesc = vUsr.description. As you might guess vUsr.description contains newlines(\n) - possibly several. Most of the time there is only one newline, so I only really care about this one. I do have other fields though with a newline(\n) at the end, so I will be using a function to call and strip whatever string I feed it, with whatever parameter I pass. I might pass it the \n or a \t or whatever else I run into. I wanted to use the Regexp, or Regexp2, since since it shows the ".*" can be used. I am hoping that it could be that simple instead. Do you use Regexp/Regexp2? if so how? What do you guys think? What do you use? How would you do it? So many questions, so little time. :) Jerry |