gsub function doesn`t replace string to nothing.

User avatar
Andrey01
Member
 
Posts: 2338
Joined: Wed Oct 19, 2016 15:18
Location: Russia, Moscow
GitHub: Andrey2470T
In-game: Andrey01

gsub function doesn`t replace string to nothing.

by Andrey01 » Sat Sep 28, 2019 21:03

The issue happens here on third line from the top:
Code: Select all
if size_str_s and player_inv_e then
      local theme_str = new_form:sub(size_str_s, player_inv_e)
      new_form = new_form:gsub(theme_str, "") -- Doesn`t replace 'theme_str' string to '' empty one.
   end


'them_str' is
Code: Select all
size[13, 9.1]tabheader[0,0;sfinv_nav_tabs;Crafting,(T@creative)AllE,(T@creative)NodesE,(T@creative)ToolsE,(T@creative)ItemsE;2;true;false]      image[5,4.7;1,1;gui_hb_bg.png]
      image[6,4.7;1,1;gui_hb_bg.png]
      image[7,4.7;1,1;gui_hb_bg.png]
      image[8,4.7;1,1;gui_hb_bg.png]
      image[9,4.7;1,1;gui_hb_bg.png]
      image[10,4.7;1,1;gui_hb_bg.png]
      image[11,4.7;1,1;gui_hb_bg.png]
      image[12,4.7;1,1;gui_hb_bg.png]
      list[current_player;main;5,4.7;8,1;]
      list[current_player;main;5,5.85;8,3;8]

That function doesn`t replace 'theme_str' to the empty string. It just drops as it is.
 

User avatar
duane
Member
 
Posts: 1596
Joined: Wed Aug 19, 2015 19:11
Location: Oklahoma City
GitHub: duane-r

Re: gsub function doesn`t replace string to nothing.

by duane » Sun Sep 29, 2019 01:18

Andrey01 wrote:That function doesn`t replace 'theme_str' to the empty string. It just drops as it is.


gsub uses patterns. Your string has pattern elements in it, so it won't replace the way a simple replace function would. You'll either have to build a simple replace or escape all those pattern elements (e.g. %] for ]).
Believe in people and you don't need to believe anything else.
 

User avatar
Andrey01
Member
 
Posts: 2338
Joined: Wed Oct 19, 2016 15:18
Location: Russia, Moscow
GitHub: Andrey2470T
In-game: Andrey01

Re: gsub function doesn`t replace string to nothing.

by Andrey01 » Mon Sep 30, 2019 18:31

duane wrote:
Andrey01 wrote:That function doesn`t replace 'theme_str' to the empty string. It just drops as it is.


gsub uses patterns. Your string has pattern elements in it, so it won't replace the way a simple replace function would. You'll either have to build a simple replace or escape all those pattern elements (e.g. %] for ]).

I`ve escaped all magic characters, but it doesn` help:
Code: Select all
local theme_tmp = "size%[13, 9%.1%]" ..
         "tabheader%[0,0;sfinv_nav_tabs;" .. table.concat(cur_context_state.nav_titles, ",") .. ";" .. cur_context_state.nav_idx .. ";true;false%]" ..
         [[
          image%[5,4%.7;1,1;gui_hb_bg%.png%]
                    image%[6,4%.7;1,1;gui_hb_bg%.png%]
                    image%[7,4%.7;1,1;gui_hb_bg%.png%]
                    image%[8,4%.7;1,1;gui_hb_bg%.png%]
                    image%[9,4%.7;1,1;gui_hb_bg%.png%]
                    image%[10,4%.7;1,1;gui_hb_bg%.png%]
                    image%[11,4%.7;1,1;gui_hb_bg%.png%]
                    image%[12,4%.7;1,1;gui_hb_bg%.png%]
                    list%[current_player;main;5,4%.7;8,1;%]
                    list%[current_player;main;5,5%.85;8,3;8%]
                     ]]
 

User avatar
duane
Member
 
Posts: 1596
Joined: Wed Aug 19, 2015 19:11
Location: Oklahoma City
GitHub: duane-r

Re: gsub function doesn`t replace string to nothing.

by duane » Tue Oct 01, 2019 02:33

Andrey01 wrote:I`ve escaped all magic characters, but it doesn` help:


I imagine you've missed some. In this case, it's going to be way too complicated to get everything escaped. I'd just do a string.find(), then use the returned location and cut the string from there to the length of your search string.

Code: Select all
function string.simple_replace(str, rep_this, rep_with)
   local l, s1, s2 = -1, '', ''

   l = str:find(rep_this, 1, true)
   if l == -1 then
      return str
   end

   if l > 1 then
      s1 = str:sub(1, l - 1)
   end

   if rep_this:len() + l <= str:len() then
      s2 = str:sub(l + rep_this:len(), str:len())
   end

   return s1 .. rep_with .. s2
end
Believe in people and you don't need to believe anything else.
 

User avatar
Andrey01
Member
 
Posts: 2338
Joined: Wed Oct 19, 2016 15:18
Location: Russia, Moscow
GitHub: Andrey2470T
In-game: Andrey01

Re: gsub function doesn`t replace string to nothing.

by Andrey01 » Tue Oct 01, 2019 19:39

duane wrote:
Andrey01 wrote:I`ve escaped all magic characters, but it doesn` help:


I imagine you've missed some. In this case, it's going to be way too complicated to get everything escaped. I'd just do a string.find(), then use the returned location and cut the string from there to the length of your search string.

Code: Select all
function string.simple_replace(str, rep_this, rep_with)
   local l, s1, s2 = -1, '', ''

   l = str:find(rep_this, 1, true)
   if l == -1 then
      return str
   end

   if l > 1 then
      s1 = str:sub(1, l - 1)
   end

   if rep_this:len() + l <= str:len() then
      s2 = str:sub(l + rep_this:len(), str:len())
   end

   return s1 .. rep_with .. s2
end

Thanks! I`ve understood what I need to do.
 


Return to Modding Discussion



Who is online

Users browsing this forum: No registered users and 2 guests