Solved

Effective/quick algorithm for changing single chars in buffer

Posted on 1997-09-16
5
230 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now