Comments (5)
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.
@floli does this answer your question?
from rapidyaml.
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.
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.
Thanks for your explanations!
from rapidyaml.
Related Issues (20)
- Request: Add more context to error traces HOT 3
- Tree::resolve() leaves some references in place in certain cases HOT 1
- Publish support for more platforms to Conan Central HOT 1
- How to get the correct col for a multiline string
- [Feature Request] Add an option to support customize installation HOT 2
- Rapidyaml calls abort(3) on random input HOT 3
- Question: filter utility use case HOT 3
- Error message 'closing ] not found' does not use _c4err macro
- An incomprehensible behavior occurred while using the for loop HOT 2
- Empty string instead of null serialized for nullptr HOT 1
- Meaning of .val()==nullptr and .has_val() HOT 2
- Namespace conflict with windows.h HOT 1
- Decimal precision truncated when serialize/deserialize std::vector<double> container HOT 2
- [Request] Make new release HOT 3
- [Bug] Text data with commas is incorrectly split HOT 4
- Serialize std::filesystem::path error HOT 16
- What is the best way to read files from ifstream in terms of performance. HOT 4
- SWIG API needs to raise exception
- How to get started when the docks are all about bragging and code examples are buried deep and inconsistent? HOT 1
- Unexpected behavior when deserializing to double HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rapidyaml.