Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Effective/quick algorithm for changing single chars in buffer

Posted on 1997-09-16
5
Medium Priority
?
247 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 400 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

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…
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 a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

604 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