Code Monkey home page Code Monkey logo

Comments (5)

biojppm avatar biojppm commented on June 12, 2024

ryml::csubstr is a string view, and as such it is NOT zero-terminated. So if you do cout << child.key().str you are printing an unbounded string (or at best bounded at the end of the full source string, if you are lucky). That's why it goes past the end of what you expect: there is no end!

Don't do that.

In your case, just do cout << child.key() (notice it does NOT have .str at the end), which will write only up to the size of the view. Likewise, cout << child.val():

#define RYML_SINGLE_HDR_DEFINE_NOW

#include "ryml_all.hpp"

#include <fstream>
#include <iostream>
#include <sstream>
#include <string>

int main() {
  std::string input{
      R"(
Sinks:
  - Type: "Colorized"
    FormatString: "my format string"
    Filter: "AAA Filter"
  - Type: "File"
    FileName: "my file name"
    Filter: "BBB Filter"
    )"};

  size_t i = 0;
  ryml::Tree tree = ryml::parse_in_arena(ryml::to_csubstr(input));
  for (ryml::ConstNodeRef const& sink : tree["Sinks"]) {
    std::cout << std::endl;
    for (ryml::ConstNodeRef const& child : sink) {
      std::cout << "Sink[" << i << "]: ~~~" << child.key() << "~~~/~~~" << child.val() << "~~~" << std::endl;
    }
    ++i;
  }

  return 0;
}

prints this:


Sink[0]: ~~~Type~~~/~~~Colorized~~~
Sink[0]: ~~~FormatString~~~/~~~my format string~~~
Sink[0]: ~~~Filter~~~/~~~AAA Filter~~~

Sink[1]: ~~~Type~~~/~~~File~~~
Sink[1]: ~~~FileName~~~/~~~my file name~~~
Sink[1]: ~~~Filter~~~/~~~BBB Filter~~~

Or you could do k = child.key(); cout.write(k.str, k.len), which is what ryml's implementation of operator<<(ostream&, ryml::csubstr) does when you call it.

from rapidyaml.

biojppm avatar biojppm commented on June 12, 2024

@floli does this answer your question?

from rapidyaml.

floli avatar floli commented on June 12, 2024

Thanks! Yes, it adds clarity. Just one more question. What is the best method to get a std::string from a node.val() or node.key() ? Is there a way that does not involve using a new variable? e.g. to be used as a parameter (f(node.key().to_str() when void f(std::string))

from rapidyaml.

biojppm avatar biojppm commented on June 12, 2024

If you want to create a std::string there must always be a new variable, as the tree holds only views. If you want syntactic candy to call your f() function without having to create a named variable, just define an adapter in your code:

std::string tostr(ryml::csubstr s) { return {s.str, s.len}; }

and then just do f(tostr(node.key())).

But note this is a performance antipattern, as the temporary std::string will cause a potential allocation and performance jitter.

I would suggest instead making your f() accept directly the ryml::csubstr, or if you don't want it to be aware of a ryml type, then just make it accept f(const char* str, size_t len) (and again note the str is not zero-terminated).

If you are wondering why ryml does not give you a std::string it's precisely because it is an antipattern forcing allocation for every string, and further there are projects in embedded where std::string just cannot be used. In fact if ryml is rapid it is because in large part it does not use std::string.

from rapidyaml.

floli avatar floli commented on June 12, 2024

Thanks for your explanations!

from rapidyaml.

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.