Remove newline ("\") from a string/buffer

Hi All,
I have seen some post regarding this, but I am not getting it for some reason.
I need something simple that will take a string and strip off the newline ("\") from it.

I am reading the users data into a buffer.
I take and assign certain elements:
Ex.
string fname = usr.fullName

I have encountered various strings to contain the newline which is causing problems in other areas.

I find that nice clear, concise, thoruough, descriptions/instructions are more beneficial that the stupid manual/reference provided by IBM.
I like to see better examples than the one's they provide, or actually don't provide for some things.
I do not understand why they would not have some osrt of TRIM function that would allow you to trim certain charaters or something. Unless I am missing something here and there is a better way.
I hope somebody can help!!
Jerry
SystemAdmin - Fri Jul 15 15:06:06 EDT 2011

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) ""

 


Result: First LineSecond Line

 

Re: Remove newline ("\") from a string/buffer
llandale - Sat Jul 16 00:51:03 EDT 2011

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) ""

 


Result: First LineSecond Line

 

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.

Better to put declare and create b2 inside the function and advertise that it should be deleted when done.

But wait!! You could cleverly alias the buffer and then don't have to worry about getting it deleted. I'm at home, but it would look something like this. Notice the change to a 'void' function:

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)

 

  • Louie

 

Re: Remove newline ("\") from a string/buffer
Mathias Mamsch - Sat Jul 16 08:45:35 EDT 2011

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.

Better to put declare and create b2 inside the function and advertise that it should be deleted when done.

But wait!! You could cleverly alias the buffer and then don't have to worry about getting it deleted. I'm at home, but it would look something like this. Notice the change to a 'void' function:

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)

 

  • Louie

 

Nit Pick:

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
Mathias Mamsch - Sat Jul 16 08:50:53 EDT 2011

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.

Better to put declare and create b2 inside the function and advertise that it should be deleted when done.

But wait!! You could cleverly alias the buffer and then don't have to worry about getting it deleted. I'm at home, but it would look something like this. Notice the change to a 'void' function:

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)

 

  • Louie

 

Nit Pick:

Don't exchange the buffer by a new one, i.e. don't make a NEW alias (invalidating all old 'aliases' or copies of the buffer handle). Better use a temporary Buffer then copy the stuff to the old buffer.

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:52:41 EDT 2011

Mathias Mamsch - Sat Jul 16 08:45:35 EDT 2011
Nit Pick:

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

Please ignore the above post (without the {c o d e} statement}, I somehow hit the wrong key which posted although I was still editing. Sorry for that! Mathias

Re: Remove newline ("\") from a string/buffer
llandale - Sat Jul 16 15:41:53 EDT 2011

Mathias Mamsch - Sat Jul 16 08:50:53 EDT 2011

Nit Pick:

Don't exchange the buffer by a new one, i.e. don't make a NEW alias (invalidating all old 'aliases' or copies of the buffer handle). Better use a temporary Buffer then copy the stuff to the old buffer.

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)


I was expecting the smaller buffers to have a greater difference because creating and deleting the temp buffer should take more time proportionally. Perhaps these results are because deleting a big buffer takes a lot more time than a small one. In any event these times are insignificant to doing DOORS chores like looping though links.

I have some objection to "setempty" but I don't recall what it is. My notes say something about inserting a null string into such a buffer afterwards messes things up; but my notes also say I could not reproduce it. I also have some objection to setting the "length(buf, 0)", and that has something to do with exporting. Also setting the first character to null set(buf, null char, 0). All these, I suspect, leave the de-allocated space still associated with the Buffer somehow. Anyway, I figure to clear a buffer with buf = "" until someone shows why that is inferior. I am conserned about the space now unused, but I see no gradual increase in memory useage so I guess we are OK there.

 

 

  • Louie

 

 

Re: Remove newline ("\") from a string/buffer
Mathias Mamsch - Sat Jul 16 17:25:28 EDT 2011

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)


I was expecting the smaller buffers to have a greater difference because creating and deleting the temp buffer should take more time proportionally. Perhaps these results are because deleting a big buffer takes a lot more time than a small one. In any event these times are insignificant to doing DOORS chores like looping though links.

I have some objection to "setempty" but I don't recall what it is. My notes say something about inserting a null string into such a buffer afterwards messes things up; but my notes also say I could not reproduce it. I also have some objection to setting the "length(buf, 0)", and that has something to do with exporting. Also setting the first character to null set(buf, null char, 0). All these, I suspect, leave the de-allocated space still associated with the Buffer somehow. Anyway, I figure to clear a buffer with buf = "" until someone shows why that is inferior. I am conserned about the space now unused, but I see no gradual increase in memory useage so I guess we are OK there.

 

 

  • Louie

 

 

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

 


Then my guess is the timing difference will not depend on the length anymore. For the static buffer outside the function, this buffer will only grow once and the other 99999 times it will already have the big capacity. As for the question of using setempty or setting the buffer content to "", these are pretty much the same. Maybe in some earlier DOORS version the setempty had problems? Regards, Mathias

 

 


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

 

Re: Remove newline ("\") from a string/buffer
SystemAdmin - Thu Jul 21 08:45:35 EDT 2011

Hi All,
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
SystemAdmin - Thu Jul 21 09:00:11 EDT 2011

Hi Agai,
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