Solved

Effective/quick algorithm for changing single chars in buffer

Posted on 1997-09-16
5
236 Views
Last Modified: 2010-04-04
I have a buffer of data (a web html page). I need to replace all occurences of eg char '100' with char '232'. I have 5 different chars all together to replace (eg. 34->33, 55->75,...).
I need a really effective algorithm for this. Delphi if good, assembler is better.
0
Comment
Question by:kolarb
  • 3
  • 2
5 Comments
 
LVL 12

Accepted Solution

by:
andrewjb earned 100 total points
ID: 1345035
How about this in assemler. It expects a PChar buffer ( terminated with a null character ) and 'change' and 'replace with' characters to swap.

If you've definitely got exactly 5 changes to make, you could do them all in one loop ( add another comment to this questions ), but that wouldn't be useful in the future ....


procedure Change( Buff : PChar ; FromChar , ToChar : char);
asm
@start:
  mov ch,[eax]
  test ch,ch              { Test for string end }
  jz @done

  cmp ch , dl             { Test for char to replace }
  jnz @Loop

  mov byte ptr [eax] , cl  { Replace with new char }
@loop:
  inc eax
  jmp @start

@done:
end;

0
 
LVL 2

Author Comment

by:kolarb
ID: 1345036
I have exactly five changes to make. I also know the exact length of the buffer. Can you incorporate that in the program?
0
 
LVL 2

Author Comment

by:kolarb
ID: 1345037
Make up the values for example purposes.
0
 
LVL 12

Expert Comment

by:andrewjb
ID: 1345038
Here you go :

( Replace the letters with character values if you want. e.g. 'a' could be replaced with 97 etc. )


To be honest, it wouldn't be too much slower in pascal.

procedure Change( Buff : PChar ; Length : integer );
asm
@start:
  test edx , edx
  jz @done

  mov cl,[eax]

@Exchange1:
  cmp cl , 'a'             { Test for char to replace }
  jnz @Exchange2
  mov byte ptr [eax] , 'b' { Replace with new char }

@Exchange2:
  cmp cl , 'e'             { Test for char to replace }
  jnz @Exchange3
  mov byte ptr [eax] , 'h' { Replace with new char }

@Exchange3:
  cmp cl , 'f'             { Test for char to replace }
  jnz @Exchange4
  mov byte ptr [eax] , 'l' { Replace with new char }

@Exchange4:
  cmp cl , 'r'             { Test for char to replace }
  jnz @Exchange5
  mov byte ptr [eax] , 'm' { Replace with new char }

@Exchange5:
  cmp cl , 'q'             { Test for char to replace }
  jnz @Loop
  mov byte ptr [eax] , 'z' { Replace with new char }


@loop:
  inc eax
  dec edx
  jmp @start

@done:
end;

0
 
LVL 2

Author Comment

by:kolarb
ID: 1345039
Thank you.
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question