Glib::Regex を使用しようとしていますが、ジャンクが返され続けます。
コードの簡略版は次のとおりです。
void testGetPos(std::string fileName){
auto regEx = Glib::Regex::create(
"^sprite_[0-9]+__x(-?[0-9]+)_y(-?[0-9]+)\\.tif$",
Glib::REGEX_CASELESS
)
Glib::MatchInfo match;
if(!regEx->match(fileName, match)){
continue;
}
if(!match.matches()){
continue;
}
auto posX = match.fetch(1);
auto posY = match.fetch(2);
// ... Use posX and posY
}
int main(){
testGetPos("sprite_000__x-28_y-32.tif");
}
これが実行された後、posX と posY はがらくたでいっぱいになります。ただし、ラップされたオブジェクトで C 関数を使用すると、次のようになります。
void testGetPos(std::string fileName){
auto regEx = Glib::Regex::create(
"^sprite_[0-9]+__x(-?[0-9]+)_y(-?[0-9]+)\\.tif$",
Glib::REGEX_CASELESS
)
GMatchInfo *match = nullptr;
if(!g_regex_match(regEx->gobj(), fileName.c_str(), (GRegexMatchFlags)0, &match)){
if(match != nullptr){
g_match_info_free(match);
}
return;
}
auto posX = g_match_info_fetch(match, 1);
auto posY = g_match_info_fetch(match, 2);
// ... Use posX and posY
g_free(posX);
g_free(posY);
g_match_info_free(match);
}
int main(){
testGetPos("sprite_000__x-28_y-32.tif");
}
正常に動作します。私は何か間違ったことをしていますか、それともこれは壊れていますか。