How do I pattern match part of string as bitstrings?

Hello Experts,

Could you please advise what would be the way to match the bitstring ?

Lets say I have and input and I want to pattern part of it as bitstsing:

@input    <<116, 114, 117, 101, 32, 105, 115, 32, 110, 111, 116, 32, 102, 97, 108, 115, 101>>
@true_    <<116, 114, 117, 101>>
@false_   <<102, 97, 108, 115, 101>>

def match_bitsting( <<true_ ::size(32) , rest :: bitstring>>= input)  when trues_ == @true_ do
    IO.puts "I got: true"
end

def match_bitsting( <<false_ ::size(40) , rest :: bitstring>>= input)  when trues_ == @false_ do
    IO.puts "I got: false"
end

How should I rewrite @true_ | @false_ or the pattern matching to make it work ?

1 Like

If I understood the problem properly, you want to match the beginning of the input string? In this case, it is easy:

def match_bitstring("true" <> _rest), do: IO.puts("true")
def match_bitstring("false" <> _rest), do: IO.puts("false")
1 Like

Not really, probably I have to change description a bit…

I have an input that looks like:

<<116, 114, 117, 101, 32, 105, 115, 32, 110, 111, 116, 32, 102, 97, 108, 115, 101, 32, 97, 110, 100, 32, 102, 97, 108, 115, 101, 32, 105, 115, 32, 110, 111, 116, 32, 116, 114, 117, 101>>

I want to do pattern match on:

<<116, 114, 117, 101>> # which is "true" and the rest data loop froward and match according other principles

Furthermore, this just dummy input just to explain the idea, the input can be any text…

@input    <<116, 114, 117, 101, 32, 105, 115, 32, 110, 111, 116, 32, 102, 97, 108, 115, 101>>
@true_    <<116, 114, 117, 101>>
@false_   <<102, 97, 108, 115, 101>>

def match_bitsting(<<@true_, rest::bits>>) do # bits is an alias for bitstring
  IO.puts "I got: true"
  match_continue(rest, true)
end

def match_bitsting(<<@false_, rest::bits>>) do
  IO.puts "I got: false"
  match_continue(rest, false)
end

Would this work for you?

Why are you using bitstrings anyway? What you have in @input is a (printable) binary.

3 Likes

Yes, this works and so clean code :open_mouth:
Somehow I overcomplicated… :hugs:

Trying to understand how the pattern matching works on bitstring level…
I mean how to use pattern matching in this type of scenario…

I am still in learning and and trying to understanding Elixir / Erlang / Pattern Matching

Thank you very much for help !

I think what @idi527 meant here, why do you have bitstring instead of binary. The first one matches arbitrary length sequences of bits while the latter matches arbitrary length sequences of bytes, and since your provided patterns all seem to stick to a byte boundary the latter might perform better then the first.

Hi Noobz

Hm… When I read some online examples it was mentioned that if you are not sure about provided input then it is better to specify bitstring rather than binary

In my case I assume the input data comes from a some API that comes as stream data.
I thought this is the way to do it.

Am I wrong ? :slight_smile: :blush:

If it is streamed data you will probably get your chunks at byte boundary (as this is the level at which your interface transfers the data) and unless the protocol specifies something that is not aligned at byte boundaries, you can safely assume binary.

BUT If the data is streamed in, you can’t safely use binary pattern matching at all but need to use a different mean of parsing/deserializing the incomming data.

What happens if your current chunk is "tru"? This does not match "true", but might after the next chunk has been received.

3 Likes

Yes, you are right. Thank you for a good point. :+1:

I thought to give a try with Genstage & Flow.

Producer:
Something like receives chunks and analyses it according to some logic.
It would split a chunk into invalid and valid parts.

  • The invalid part would buffered until next chunk & analysis.
  • The valid part would be available to ConsumerProducer.

ConsumerProducer:

  • Receives a valid chunk and does the pattern matching.
  • Does some manipulations and prepares data for the next stage.

Consumer:

  • Gets final data produces some output.
1 Like

This is a valid approach but only if order of processing of the datagrams does not matter.

1 Like

Probably the implementation itself would be changed later on, but definitely I would need to consider more nuances :slight_smile:

At this point in time I just want get better understanding of Genstage, Flow, Stream & How to glue all together :smile:

But you already gave me valuable information to think of, thank you very much for that :slight_smile:

1 Like

@DimWell Just for the record, what you were trying to do is possible, you just need to use the suffix ‘bits-’ or ‘bytes-’…

<<x::bits-size(16), _::binary>> = "true"
inspect(x) # "tr"

<<x::bytes-size(2), _::binary>> = "true"
inspect(x) # "tr"

;D

2 Likes