@@ -33,28 +33,36 @@ class A2uiStreamParserV09 : public A2uiStreamParserImpl {
3333
3434protected:
3535 void sniff_metadata () override {
36- auto get_latest_value = [this ](const std::string& key) -> std::string {
37- std::regex pattern (R"( \")" + key + R"( \"\s*:\s*\"([^\"]+)\")" );
38- std::smatch match;
39- std::string search_str = json_buffer_;
40- std::string result = " " ;
41- while (std::regex_search (search_str, match, pattern)) {
42- result = match[1 ].str ();
43- search_str = match.suffix ().str ();
36+ static const std::regex sid_pattern (R"( \"surfaceId\"\s*:\s*\"([^\"]+)\")" );
37+ static const std::regex root_pattern (R"( \"root\"\s*:\s*\"([^\"]+)\")" );
38+
39+ auto get_latest_value = [this ](std::string_view key, const std::regex& pattern) -> std::string_view {
40+ std::string pattern_str = " \" " + std::string (key) + " \" " ;
41+ size_t pos = json_buffer_.rfind (pattern_str);
42+ while (pos != std::string::npos) {
43+ std::smatch match;
44+ auto start = json_buffer_.cbegin () + pos;
45+ auto end = json_buffer_.cend ();
46+ if (std::regex_search (start, end, match, pattern)) {
47+ return std::string_view (json_buffer_.data () + pos + match.position (1 ), match.length (1 ));
48+ }
49+ if (pos == 0 ) break ;
50+ pos = json_buffer_.rfind (pattern_str, pos - 1 );
4451 }
45- return result ;
52+ return " " ;
4653 };
4754
48- std::string sid = get_latest_value (" surfaceId" );
55+ std::string_view sid = get_latest_value (" surfaceId" , sid_pattern );
4956 if (!sid.empty ()) surface_id_ = sid;
5057
51- std::string rid = get_latest_value (" root" );
58+ std::string_view rid = get_latest_value (" root" , root_pattern );
5259 if (!rid.empty ()) root_ids_[surface_id_] = rid;
5360
54- auto check_msg_type = [this ](const std::string& type) {
55- if (json_buffer_.find (" \" " + type + " \" :" ) != std::string::npos) {
61+ auto check_msg_type = [this ](std::string_view type) {
62+ std::string search_str = " \" " + std::string (type) + " \" :" ;
63+ if (json_buffer_.find (search_str) != std::string::npos) {
5664 if (std::find (msg_types_.begin (), msg_types_.end (), type) == msg_types_.end ()) {
57- msg_types_.push_back (type);
65+ msg_types_.push_back (std::string ( type) );
5866 }
5967 active_msg_type_ = type;
6068 }
@@ -146,7 +154,7 @@ class A2uiStreamParserV09 : public A2uiStreamParserImpl {
146154 return obj.contains (MSG_TYPE_CREATE_SURFACE) || obj.contains (MSG_TYPE_UPDATE_COMPONENTS) || obj.contains (MSG_TYPE_UPDATE_DATA_MODEL);
147155 }
148156
149- std::string get_active_msg_type_for_components () const override {
157+ std::string_view get_active_msg_type_for_components () const override {
150158 if (!active_msg_type_.empty ()) return active_msg_type_;
151159 for (const auto & mt : msg_types_) {
152160 if (mt == MSG_TYPE_UPDATE_COMPONENTS || mt == MSG_TYPE_CREATE_SURFACE) {
@@ -157,7 +165,7 @@ class A2uiStreamParserV09 : public A2uiStreamParserImpl {
157165 return msg_types_.empty () ? " " : msg_types_[0 ];
158166 }
159167
160- std::string get_data_model_msg_type () const override {
168+ std::string_view get_data_model_msg_type () const override {
161169 return MSG_TYPE_UPDATE_DATA_MODEL;
162170 }
163171
0 commit comments