Giter Club home page Giter Club logo

Comments (4)

unknwon avatar unknwon commented on August 16, 2024

Good catch.

Also I think the inline comment breaks something indeed...

from ini.

unknwon avatar unknwon commented on August 16, 2024

OK, just refactored the parser, please try it out.

The save result for

key1 = "value" ; comment
key2 = "one", "two", "three"

is now

; comment
key1 = value
key2 = "one", "two", "three"

from ini.

Timmmm avatar Timmmm commented on August 16, 2024

I dunno this still seems like it might not be that robust (though writing a truely robust INI parser is probably impossible).

I decided to modify your library fairly drastically, check it out:

https://github.com/Timmmm/ini/tree/master

You probably don't want to use that because I removed several features I don't need (e.g. variable substitution). But I think my parsing approach is probable better. Here's the approach:

  1. Remove multi-line support as this makes it a lot easier (you can still do multiline strings with "a\nb").

  2. Add a modified boolean to all the keys. This indicates whether they have been modified since loading from disk (or string) (TODO!)

  3. When parsing a key (or a section header), only try to split it into the key name, value and comment. In particular I don't try to parse the value. To split the comment I use made a couple of functions that find the first unquoted character in a string. So for example it you can do:

    letters = "a; b; c"  ; Semicolon separated list of letters.
    

    Would that break in your parser? Currently the quoting model used is very basic (only double quotes for example). You could add support for """ and single quotes.

  4. Instead of parsing the values when you load the file, parse them when they are requested. This also has the benefit of allowing users to do their own custom parsing.

  5. When writing back to the file, if the value was unmodified write it as-is (also TODO!)

I also added support for trailing comments. Feel free to take any code back!

from ini.

unknwon avatar unknwon commented on August 16, 2024

You probably don't want to use that because I removed several features I don't need (e.g. variable substitution). But I think my parsing approach is probable better. Here's the approach:

That's the best thing about open source ๐Ÿ˜‰

Remove multi-line support as this makes it a lot easier (you can still do multiline strings with "a\nb").

Thought I agree it is not a commonly used feature, but as a public package remove a feature generally should not happen.

Add a modified boolean to all the keys. This indicates whether they have been modified since loading from disk (or string) (TODO!)

This is indeed a good thing, this allows you save the config file only for new additions but remain unchanged for everything else.

When parsing a key (or a section header), only try to split it into the key name, value and comment. In particular I don't try to parse the value. To split the comment I use made a couple of functions that find the first unquoted character in a string. So for example it you can do:

Quotes will be removed is documented explicitly in the README, so it causes break changes.

Instead of parsing the values when you load the file, parse them when they are requested. This also has the benefit of allowing users to do their own custom parsing.

It should work well in most of cases, but in my personal usages, it hurts big performance (maybe I should say unnecessary cost). For example, I use INI as I18N parse module, it is called so often in when rendering every page.

from ini.

Related Issues (20)

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.