Giter VIP home page Giter VIP logo

bencode's People

Contributors

cmiles74 avatar felixr avatar greenfork avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

felixr

bencode's Issues

Convert keywords to strings when encoding

There's a suggestion to allow converting structures such as {:key :val} or [:done :error] to bencode. Currently it complains that it cannot convert keywords. Other implementations I saw (particularly inspired by clojure implementation) convert keywords to strings in this case.

I can see a reasoning that it is not allowed in that the type information is lost during this conversion and you can't guarantee that

(= str (decode (encode str)))

So maybe one option is to hide this behind a flag such as strict-conversion. Or maybe it's not that important and always convert keywords to strings. Converting to strings is definitely more convenient for the casual use case, but there's also a comment on why bencode will never support floating point numbers which says differently https://lists.ibiblio.org/pipermail/bittorrent/2004-August/001040.html.

Implementations:

If you are okay with the idea, I can suggest and provide a pull request for the following:

  • Add strict-conversion flag to all the public write functions, true by default so the default follows specification (probably keyword-style as in #3 if that gets accepted)
  • When this is false, convert keywords and symbols to strings

Immutable vs mutable data structures

Hi, I really enjoy your library! I have a question about the choice of mutable data structures for representation, why was the decision to choose them instead of immutable ones?

My main use case is that buffers @"abc" can't be used in comparisons easily, meaning that (= @"a" @"a") prints false. This makes it impossible to use in expressions such as match and probably others.

I use this function to change everything to immutable structures currently but I wonder if there are any ideas of changing the default one to immutable too:

(defn deep-immutabilize
  "Turn every mutable structure to immutable one."
  [value]
  (defn immutabilize [v]
    (match (type v)
      :struct (deep-immutabilize v)
      :table (deep-immutabilize v)
      :buffer (string v)
      :array (map immutabilize v)
      :tuple (map immutabilize v)
      _ v))
  (def t (type value))
  (cond
    (or (= :table t) (= :struct t))
    (struct ;(mapcat
              (fn [x]
                (let [first (0 x) second (1 x)]
                  [(keyword first) (deep-immutabilize second)]))
              (pairs value)))
    (or (= :array t) (= :tuple t))
    (tuple ;(map deep-immutabilize value))
    (immutabilize value)))

Use keyword-style arguments for read functions

If #2 gets merged, there will be already 3 optional parameters to the read functions. They all create some kind of specialized behavior. I usually see such cases utilizing the keyword-style arguments https://janet-lang.org/docs/functions.html.

I would suggest to use this style for all public read functions: read, read-buffer and read-stream. The reason is just a more convenient API for the library. If you think this is good, I can prepare a pull request.

read-dictionary and read-list read additional byte from stream

In read-dictionary and read-list the code uses match-byte to check that the value is properly terminated:

(if-not (match-byte reader-in END-FLAG)

But match-byte also advances to the next byte in the stream. If this is a network stream we possibly wait forever to get a next byte and never return the fully parsed value.

A quick fix is to just use peek-byte when we are parsing a top-level list or dictionary. I implemented this in
felixr@c4c59e0

It looks like it is working, but I admittedly have not tried hard to make it nice and went with the first thing that came to my mind.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.