Z Escape Sequence

Andres Suarez pointed me to some interesting code in the Hack codebase:

let slash_escaped_string_of_path path =
  let buf = Buffer.create (String.length path) in
  String.iter (fun ch ->
    match ch with
    | '\\' -> Buffer.add_string buf "zB"
    | ':' -> Buffer.add_string buf "zC"
    | '/' -> Buffer.add_string buf "zS"
    | '\x00' -> Buffer.add_string buf "z0"
    | 'z' -> Buffer.add_string buf "zZ"
    | _ -> Buffer.add_char buf ch
  ) path;
  Buffer.contents buf

What it does is to turn all the occurrences of \, :, /, \0 and z into zB, zC, zS, z0 and zZ. This way, there won't be any of those characters in the original string which are probably invalid in the context where that string is transported. But you still have a way to get them back by transforming all the z-sequences back to their original form.

Why is it useful?

The first interesting aspect about it is that it's using z as an escape character instead of the usual \. In practice, it's less likely for a string to contain a z rather than a \ so we have to escape less often.

But the big wins are coming when escaping multiple times. In the \ escape sequence, it looks something like this:

  • \ -> \\ -> \\\\ -> \\\\\\\\ -> \\\\\\\\\\\\\\\\

whereas with the z escape sequence:

  • z -> zZ -> zZZ -> zZZZ -> zZZZZ

The fact that escaping a second time doubles the number of escape characters is problematic in practice. I was working on a project once where we found out that the \ character represented 70% of the payload!

Conclusion

It's way too late to change all the existing programming languages to use a different way to escape characters but if you have the opportunity to design an escape sequence, know that \ escape sequence is not always the best 🙂

If you liked this article, you might be interested in my Twitter feed as well.
 
  • 新年好呀,新年好呀,祝福博主新年好!

  • 我有强迫症,看到好文就想顶!

  • 转眼又是新一年,博主万事如意不差钱!

  • 不错!不错!感觉好极了!

  • 看看新闻就有钱赚,完全免费月赚八千!

    http://reg.cseyzx.com/notem/zczc.html?id=A54688091

    趣头条

  • 不错不错!内容感觉好极了!

  • 新春佳节到。祝好!祝好!

  • 鸟儿叫,花儿笑,一年一季春来到!

  • 新年虽已过,好运不打折!

  • 受益匪浅,感触良多!

  • 我踩故我在,下次还会来!

  • 为啥我又来,因为这里百看不厌!

  • 日看博客两三篇,每天生活乐无边!

  • 一个博客能让人流连忘返,讲真,靠的是实力!

  • 增大壮阳、丰乳缩阴、泡妞把妹,价值十万资料无限下。

    逆向推荐,上线替下线赚钱,什么不用干,坐收二百万。

    增大网:

    111.zhuan.in

    抓紧来!

  • 人不学不知义!特来学习学习!

  • 让人百看不厌的博客,真的不是很多!

  • 948

    炎热夏天,闲来无事,拜读博客,消暑解闷!

  • 从百度点进来的,学习学习,呵呵!

  • 到你的博客走一趟,如同阳光洒在我脸上,心里暖洋洋!

  • 这里真心不错,每次来都有新收获!

  • 呵呵。学习了。感触良多!

  • 学无止境,认真拜读!

  • 来看看,因为,总能学到东西!

  • 增大壮阳!丰乳缩阴!泡妞把妹!房中秘术!

    够硬!男人的把柄才能堵住女人的漏洞!

    什么不用干,不用拉下线,坐收二百万!

    注册网址:

    earn.948.ooo

    吊炸天!

 

Related Posts

  • August 27, 2011 Start a technical blog, it’s worth it! (4)
    Lately, I've been advocating to all my student friends to start a blog. Here's an article with the most common questions answered :) What are the benefits? Being known as an expert. The majority of my blog posts are about advanced Javascript topics. As a result, I'm being tagged as […]
  • August 4, 2009 Project – Shortest Path (0)
    A school project was to find the shortest path in a dungeon graph. You start with an amount of hit points, and each edge gives or removes hit points. You have to find the path from two points going through the minimum of edges (no matter their value) alive (hp > 0 all along the path). […]
  • September 22, 2011 URLON: URL Object Notation (43)
    #json, #urlon, #rison { width: 100%; font-size: 12px; padding: 5px; height: 18px; color: #560061; } I am in the process of rewriting MMO-Champion Tables and I want a generic way to manage the hash part of the URL (#table__search_results_item=4%3A-slot). I no longer […]
  • October 12, 2011 Hook document.CreateElement (3)
    For a project I will talk later on, I need to hook the function document.createElement. The code I wanted to write was: var original = document.createElement; document.createElement = function (tag) { // Do something return original(tag); }; Problem However, there's a […]
  • August 22, 2009 Fief (Board Strategy Game) Analysis (0)
    While on holidays, I played that old (bought in the 80's) board game called Fief. After several games I wanted to point at the good and bad things about this game in order to find some general rule. Some assets of the game are innovative and worth mentioning. The resources are […]