News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Remove LF from a string

Started by SCHiM, October 23, 2010, 05:54:54 PM

Previous topic - Next topic

SCHiM

Hello everyone,
I'm trying to empty the Linefeed charachter from a string
The code compiles & runs fine. I have ran it in a debugger to see if it worked correctly, and it did
A bit further in my code I have a message box, that is to output my string to the screen, but the message box is empty
Here's the code I'm using:

xor esi, esi ; empty our counter

NCLRF:


cmp [buffer + esi], 10d  ; check for a CRLF charachter
je Rmove                 ; if we find own replace it with a 0

inc esi

cmp esi, sizeof buffer   ; compare our counter with the size of our buffer
jne NCLRF                ; they don't equal? then do all this stuff again

invoke MessageBox, 0, addr buffer, addr capt, MB_OK
             ;¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
             ;buffer is ready to have a PRVMS: command appended to it (no \n left)
             ;¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
               

jmp loopspt            ; jump back to our main loop

Rmove:
mov [buffer + esi], 0  ; place a 0 in the buffer
inc esi                ; increment our counter
jmp NCLRF              ; back to the loop


Could someone please tell me what I'm doing wrong and how to fix it, it might be a verry stupid mistake, since I'm very tired atm but I don't want to stop coding  :toothy

ramguru

Why are you placing 0 instead of \n :D ? It's a string terminator. If you really want to get rid of \n characters, you'll have to shift the buffer by one character (assuming it's only \n not \r\n) backwards when \n is found or at least put space character for a start.

SCHiM

Quote from: ramguru on October 23, 2010, 06:03:20 PM
Why are you placing 0 instead of \n :D ? It's a string terminator. If you really want to get rid of \n characters, you'll have to shift the buffer by one character (assuming it's only \n not \r\n) backwards when \n is found or at least put space character for a start.

Wa? 0's.... 0's a... string terminator? "-_-

*gets some coffee*
No stricke that
*Gets a dubble coffee*

Yea, now we're talking
But with what should I replace it them? I mean I have more data following that \n character, I cannot shift my buffer...
How would you go about it?

edit:

Ok, I've replaced the 0 with a 1, and that seems to be working, but are there any better ways of doing this?

ramguru

Quote from: SCHiM on October 23, 2010, 06:07:50 PM

edit:

Ok, I've replaced the 0 with a 1, and that seems to be working, but are there any better ways of doing this?

:bdg  :lol  :U  :green

Space character, you know: mov [buffer + esi], ' '

Or you could have, say, two buffers:
Buffer1 the source buffer
Buffer2 the one that you copy everything to except \n characters
So you wouldn't need to replace anything

SCHiM

Quote from: ramguru on October 23, 2010, 06:16:08 PM

:bdg  :lol  :U  :green

Space character, you know: mov [buffer + esi], ' '

Or you could have, say, two buffers:
Buffer1 the source buffer
Buffer2 the one that you copy everything to except \n characters
So you wouldn't need to replace anything

:dazzled:

*Tired-O-meter: spikes*
I must really be half asleep to not have though about using a space  :eek
Anyways, I thank you
It's working now

As for the 2nd method, I don't know before hand how big my buffer is going to be
I know it will be less then 1kb big but creating 2 buffers 2 kb's big while most of the time only 100 or 200 bytes are going to be used is a waste :P

clive

Use two pointers to the same buffer, only advance the second if you copy a non CR/LF character.

strloop:
  mov al,[esi]
  or al,al
  jz done
  cmp al,0Ah
  jz strloop
  mov [edi],al
  inc edi
  jmp strloop
done:

Also you might want to check for NUL characters in the input stream if the string does not fill the entire buffer.
It could be a random act of randomness. Those happen a lot as well.

raymond

Quotestrloop:
  mov al,[esi]
  or al,al
  jz done
  cmp al,0Ah
  jz strloop
  mov [edi],al
  inc edi
  jmp strloop
done:

I would modify this a bit to insure the terminating 0 is also copied at the end of the modified string:

strloop:
  mov al,[esi]
  cmp al,0Ah
  jz strloop
  mov [edi],al
  inc edi
  or al,al
  jnz strloop
;done
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

hutch--

You normally write code like this to change CRLF delimited text to wordwrap format and you do it by testing for both 13 and 10 in a collector loop and on exit write 1 space. If you don't want 1 trailing space you test the last byte written and if its ascii 32 (a space) you decrement the write pointer by 1 and write the terminating zero at that location.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php