Solved

Effective/quick algorithm for changing single chars in buffer

Posted on 1997-09-16
5
241 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

690 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