Solved

Effective/quick algorithm for changing single chars in buffer

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

939 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

10 Experts available now in Live!

Get 1:1 Help Now