tboox / tbox

🎁 A glib-like multi-platform c library
https://docs.tboox.org
Apache License 2.0
4.89k stars 725 forks source link

tb_oc_string_init_from_cstr没有将字符串中的\n转义为\\n #223

Closed duyanning closed 12 months ago

duyanning commented 1 year ago

描述问题

tb_oc_string_init_from_cstr 也许准确说是tb_object_writ_to_data(obj_msg, bufJson, sizeof bufJson, TB_OBJECT_FORMAT_JSON) 没有将字符串中的\n转义为\\n

期待的结果

json对象的字符串字段,如果该字符串中含有单个字符\n, 我希望在产生的json对象中,单个字符\n被转换为两个字符串:即单个字符\\与 单个字符n

错误信息

导致得到的字符串中含有\n而不是\\n 如下(注意pubKey字段,这个字符串中含有多个不可见的\n,它们应该是可见的\\n) { "name": "REGISTER" , "pubKey": "-----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxtQ78zfTJOPhN/5jmX1k pU7ELTPggvZjbeyti91j2ZiQfYm9FeS2qXrkrTwkDZpHT1XgPlcHutz+QhCAJ1ej J1+1w3wtp/Mqho7JVEUXFlqPBxhVNYaeuJrxX3lk2jqwNHW/vk7PO/dDn8r1EDrV FQckEWI2X2T5GP9bV4fEnUIHzZxHyucJGAf0kxQUxZ0F0x1NRQ9z6ula2RKsB48i

相关环境

请提供编译和运行环境信息。

其他信息

请提供其他附加信息帮助我们诊断问题。

Issues-translate-bot commented 1 year ago

Bot detected the issue body's language is not English, translate it automatically.


Title: tb_oc_string_init_from_cstr did not escape the \n in the string as \n

Describe the problem

tb_oc_string_init_from_cstr does not escape the \n in the string as \n

Expected results

The string field of the json object, if the string contains a single character \n, I hope that in the resulting json object, a single character \n is converted into two strings: a single character \ and a single character n

Error message

Causes the resulting string to contain \n instead of \n As follows (note the pubKey field, this string contains multiple invisible\n, they should be visible\n) { "name": "REGISTER" , "pubKey": "-----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxtQ78zfTJOPhN/5jmX1k pU7ELTPggvZjbeyti91j2ZiQfYm9FeS2qXrkrTwkDZpHT1XgPlcHutz+QhCAJ1ej J1+1w3wtp/Mqho7JVEUXFlqPBxhVNYaeuJrxX3lk2jqwNHW/vk7PO/dDn8r1EDrV FQckEWI2X2T5GP9bV4fEnUIHzZxHyucJGAf0kxQUxZ0F0x1NRQ9z6ula2RKsB48i

Related environment

Please provide compilation and runtime environment information.

other information

Please provide any additional information to help us diagnose the problem.

waruqi commented 1 year ago

之前没做转义,刚稍微搞了下,可以试试https://github.com/tboox/tbox/commit/a83fd7736b538153191e144cc87418d08701a556#diff-450eb88d32029322cda729d0ea85f19d3c31d09003458652a51fef7cb3bb3ee1

最近也没时间细搞,如果有问题,可以自己提 pr 过来改进

Issues-translate-bot commented 1 year ago

Bot detected the issue body's language is not English, translate it automatically.


I didn’t do escaping before, just made a little effort, you can try https://github.com/tboox/tbox/commit/a83fd7736b538153191e144cc87418d08701a556#diff-450eb88d32029322cda729d0ea85f19d3c31d09003458652a 51fef7cb3bb3ee1

I don’t have time to work on it recently. If you have any problems, you can submit a pr to improve it.