【问题标题】:Why lists:filter doesn't work well?为什么列表:过滤器不能正常工作?
【发布时间】:2014-04-19 14:27:55
【问题描述】:

使用postgresql的sql,从其他文档中得知,为了防止sql注入攻击,需要过滤几个特殊的关键字,比如', ;, &, 。

quote(Value) when is_integer(Value)->
    Value;
quote(Value) ->
    %% seperate_by(["'",Value,"'"],"").
    Value_a = lists:dropwhile(fun($')->true;
                 ($;)->true;
                 ($<)->true;
                 ($>)->true;
                 ($&)->true;
                     (_)->false
                      end,Value),
    seperate_by(["'",Value_a,"'"],"").

(emacs@yus-iMac.local)62> john_worker:quote("<>&asdf'").
"'asdf''"
(emacs@yus-iMac.local)63> john_worker:quote("'asdf").
"'asdf''"
(emacs@yus-iMac.local)64> john_worker:quote("'asdf").
"'asdf'"
(emacs@yus-iMac.local)65> john_worker:quote("'asdf").
"'asdf'"
(emacs@yus-iMac.local)66> john_worker:quote("a'sdf").
"'a'sdf'"
(emacs@yus-iMac.local)67> john_worker:quote("a>sdf").
"'a>sdf'"

lists:filter 适用于以这些特殊字符为前缀的单词,但不适用于其他条件。为什么?

【问题讨论】:

  • lists:dropwhile 将丢弃头部,直到谓词返回 false - 这与您的结果一致。我认为lists:filter 是您想要的,但是您必须反转当前谓词的布尔返回值。

标签: erlang


【解决方案1】:

我不确定您期望的结果是什么,如果您只是想跳过这些特殊字符,您可以使用列表推导:

quote(Value) ->
     "'" ++ [X || X <- Value , X =/= $', X =/= $;, X =/= $<, X =/= $>, X =/= $&] ++ "'".

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-01
    • 2019-02-26
    • 2023-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多