Clojure读取字符串
(读取字符串“01”)返回1 事实上,从“01”到“07”的读取字符串返回正确答案。但是,当我们这样做(读取字符串“08”)时,它抛出了一个错误: NumberFormatException无效编号:08 clojure.lang.LispReader.readNumber(LispReader.java:330)Clojure读取字符串,clojure,Clojure,(读取字符串“01”)返回1 事实上,从“01”到“07”的读取字符串返回正确答案。但是,当我们这样做(读取字符串“08”)时,它抛出了一个错误: NumberFormatException无效编号:08 clojure.lang.LispReader.readNumber(LispReader.java:330) 谁能帮我弄清楚原因吗?如果你一直这样做,你可能会看到: > (read-string "010") 8 以0开头的文字数字被视为八进制(以8为基数)。因此数字8无效。如果字符
谁能帮我弄清楚原因吗?如果你一直这样做,你可能会看到:
> (read-string "010")
8
以
0
开头的文字数字被视为八进制(以8为基数)。因此数字8
无效。如果字符串以0开头,则该数字将被读取为八进制数字:
因此,下表提供了一些示例转换:
- 01->1
- 02->2
- 0117->79
- 0200->128
- 08->无效
user> 010
8
或指定一个基准:
user> 8r010
8
user> 2r010
2
user> 16r010
16
user> 17r010
17
或者您可以使用后缀N
来读取前导为零或不为零的bigint
user> 010N
8N
user> 10N
10N
尽管这不适用于基数前缀:
user> 2r1010N
NumberFormatException For input string: "1010N"
当然,默认情况下,读取字符串是完全不安全的
user> #=(slurp "http://example.com")
"<!doctype html>\n<html>\n<head>\n <title>Example Domain</title>\n\n <meta charset=\"utf-8\" />\n <meta http-equiv=\"Content-type\" content=\"text/html; charset=utf-8\" />\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n <style type=\"text/css\">\n body {\n background-color: #f0f0f2;\n margin: 0;\n padding: 0;\n font-family: \"Open Sans\", \"Helvetica Neue\", Helvetica, Arial, sans-serif;\n \n }\n div {\n width: 600px;\n margin: 5em auto;\n padding: 50px;\n background-color: #fff;\n border-radius: 1em;\n }\n a:link, a:visited {\n color: #38488f;\n text-decoration: none;\n }\n @media (max-width: 700px) {\n body {\n background-color: #fff;\n }\n div {\n width: auto;\n margin: 0 auto;\n border-radius: 0;\n padding: 1em;\n }\n }\n </style> \n</head>\n\n<body>\n<div>\n <h1>Example Domain</h1>\n <p>This domain is established to be used for illustrative examples in documents. You may use this\n domain in examples without prior coordination or asking for permission.</p>\n <p><a href=\"http://www.iana.org/domains/example\">More information...</a></p>\n</div>\n</body>\n</html>\n"
有一个神奇的咒语可以关闭读取评估,作为一个全局状态,其他库可以在不告诉您的情况下打开它,因此不要依赖于此,在所有情况下都使用clojure.edn/read-string
是的,我已经闯入了一个真实的生产网站,使用read string作为授权专业安全渗透测试的一部分。这个问题是关于read string函数的,它比标记为重复的问题稍微具体一些。read string有重要的安全注意事项,值得注意。大多数情况下在用户提供的输入上使用它是完全不安全的
user> (clojure.edn/read-string "2r1010")
10