From 94fcdebf685deac31c1d55a7e66d787ba8301cdf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Niemier?= Date: Wed, 26 Jun 2024 17:11:07 +0200 Subject: [PATCH] ft: improve some decoding paths --- lib/supavisor/protocol/client.ex | 16 ++++++---------- lib/supavisor/protocol/server.ex | 4 ++-- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/lib/supavisor/protocol/client.ex b/lib/supavisor/protocol/client.ex index b9a3c966..dc78e08f 100644 --- a/lib/supavisor/protocol/client.ex +++ b/lib/supavisor/protocol/client.ex @@ -103,22 +103,18 @@ defmodule Supavisor.Protocol.Client do end def decode_payload(:simple_query, payload) do - case String.split(payload, <<0>>) do + case :binary.split(payload, <<0>>) do [query, ""] -> query _ -> :undefined end end + def decode_payload(:parse_message, <<0>>), do: :undefined + def decode_payload(:parse_message, payload) do - case String.split(payload, <<0>>) do - [""] -> - :undefined - - other -> - case Enum.filter(other, &(&1 != "")) do - [sql] -> sql - message -> message - end + case :binary.split(payload, <<0>>, [:global, :trim_all]) do + [sql] -> sql + message -> message end end diff --git a/lib/supavisor/protocol/server.ex b/lib/supavisor/protocol/server.ex index ac96d571..c2986e24 100644 --- a/lib/supavisor/protocol/server.ex +++ b/lib/supavisor/protocol/server.ex @@ -172,7 +172,7 @@ defmodule Supavisor.Protocol.Server do # https://www.postgresql.org/docs/current/protocol-error-fields.html def decode_payload(:error_response, payload) do - String.split(payload, <<0>>, trim: true) + :binary.split(payload, <<0>>, [:global, :trim_all]) end def decode_payload( @@ -195,7 +195,7 @@ defmodule Supavisor.Protocol.Server do end def decode_payload(:password_message, "md5" <> _ = bin) do - case String.split(bin, <<0>>) do + case :binary.split(bin, <<0>>) do [digest, ""] -> {:md5, digest} _ -> :undefined end