简体   繁体   中英

String Encoder Using Multiple Global String Substitution

I'm trying to write up a simple encoding scheme. As a basic example of what I'm after, let's say there is a string 1001. I want to replace the the 0's and 1's as

0 => 01

1 => 10

so that 1001 gets encoded to 10010110.

If I were doing such a replacement in an editor like vim, doing one global replace after the other, I would do a hack where I substitute the starting characters with other characters not involved in the substitution (like change 0 to x and 1 to y). That way the product of the first substitution is protected from the subsequent substitutions.

1001 => yxxy

yxxy => y0101y (I don't want to replace the 1's that result from the first substitution)

y0101y => 10010110

Generalizing to n replacements and doing this in code, it seems like a straight forward method would be to put the string in a character array and do a substitution per cell. Then each substitution is independent of one another.

For a sufficiently large string this could be slow. What I don't know is if generic string replace methods or functions or apis are faster, or if under the hood they also utilize an array. If there's no special magic to let them do a substitution of a million characters in 1 operation, then it doesn't matter. But if they are faster, how can I do generic substitutions for n cases while protecting the results from further replacement? Is there an accepted algorithm?

This would then extend to multiple string regex of varying size. If I had to replace aba, cdee, etc, it seems like I couldn't put the target string into a data container in a logical manner. Is making such an encoder beyond the scope of simple string substitutions?

As long as you don't have to transcode in place just have an input array/string/file and an output one. Read from input, transcode, and write to output. This way all replacements would be done in a single pass.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM