-- LuaHUB Discussion Forum - Trim unicode string -- http://forum.luahub.com/index.php?topic=3522.0 function chopup(str) local ret={} local pos = 1 local b=string.byte local f=math.floor while pos <= #str do local first = b(str:sub(pos,pos)) if first < 0x00000080 then -- ASCII ret[#ret+1]= str:sub(pos,pos) pos = pos+1 elseif f(first/2^5) == 6 then -- 0b110 then -- Two byte UTF ret[#ret+1]= str:sub(pos,pos+1) pos = pos+2 elseif f(first/2^4) == 14 then --0b1110 then -- Three byte UTF ret[#ret+1]= str:sub(pos,pos+2) pos = pos+3 elseif f(first/2^3) == 46 then--0b11110 then -- Four byte UTF ret[#ret+1]= str:sub(pos,pos+3) pos = pos+4 elseif f(first/2^2) == 110 then --0b111110 then -- Five byte UTF ret[#ret+1]= str:sub(pos,pos+4) pos = pos+5 elseif f(first/2) == 238 then --0b1111110 then -- Six byte UTF ret[#ret+1]= str:sub(pos,pos+5) pos = pos+6 else print("first byte faulty") pos=pos+1 end end return ret end ------------------------------- function sub_utf (str,x,...) local chp = chopup(str) local lngth = #chp local new = "" local y = select(1,...) if y == nil then y = lngth end if type(y) =='number' and y <0 then y = lngth + y +1 end if type(x) =='number' and x <0 then x = lngth + x +1 end if x >lngth or y >lngth then return nil end if type(x) =='number' and type(y) =='number' and x ~=0 and y ~=0 and x <= y then for i = x,y do new = new .. tostring(chp[i]) end elseif type(x) =='number' and type(y) =='number' and x ~=0 and y ~=0 and x > y then for i= x,y,-1 do new = new .. tostring(chp[i]) end end return new end function cut_utf (str,separator,direction) local chp = chopup(str) local lngth = #chp local ini = 1 if direction == -1 then ini = -1 end local n if ini == 1 then for i = 1,lngth do if chp[i] == separator then n = i break end end elseif ini == -1 then for i = lngth,1,-1 do if chp[i] == separator then n = i break end end end if n == nil then return nil,str,nil else return n,sub_utf(str,1,n-1),sub_utf(str,n+1,lngth) end end -- For one-to-one injective,sample_name to sample_mapping. -- Return the table in whitch a word-key have a table -- ,{['note']=note_number -- ,['velo']=velocity_number -- ,['inst']=instrument_index} -- num_letters:max number of letters for making key-strings -- from sample names. -- init_idx:initial table function name2note_velo_idx (inst_idx,num_letters,init_idx) local maps = init_idx for i,v in pairs (renoise.song().instruments[inst_idx] .sample_mappings[1]) do local _name = renoise.song().instruments[inst_idx] .samples[v.sample_index].name local choped_name = chopup(_name) -- from 'unicode_utilities' local len = #choped_name if len > num_letters then len = num_letters end _name = table.concat(choped_name) local n,_name = cut_utf(sub_utf(_name,1,len),' ') -- separater: space local note = v.note_range[1] local velo = v.velocity_range if velo[1]==0 and velo[2]==127 then velo = 255 else velo = velo[2] end local tbl ={} tbl['note'] = note tbl['velo'] = velo tbl['inst'] = inst_idx maps[_name] = tbl end return maps end --[[ 入れ子のTableの中身も追って、表示。 -- 深さに応じたtab情報 k 付の表示用関数を再帰させる。 --]] function c_tbl (table) local function form1 (x) if type (x) == "string" then x = string.format('%q',x) return x elseif type (x) == "number" then return x else x = tostring (type(x)) return x end end local function see_k (tbl,k) for index,val in pairs(tbl) do if type(val) == "table" then print(string.rep("\t",k).."["..form1(index).."]".."={") see_k(val,k+1) print(string.rep("\t",k+1).."}") else print(string.rep("\t",k).."["..form1(index).."]".."="..form1(val)) end end end see_k(table,1) end --test test=name2note_velo_idx (renoise.song().selected_instrument_index,6,{}) print("< "..renoise.song().selected_instrument.name.." Key List >") c_tbl(test)