Modules-old/cutil/lua_cutil.cpp
/* Copies string data to the torch tensor */
Copies string data to the torch tensor
Copies string data to the torch tensor
static int lua_string2tensor(lua_State *L) {
  size_t n_str;
  char * src = (char*)lua_tolstring(L, 1, &n_str);
  if( src==NULL )
    return luaL_error(L, "Bad source string.");
  const char* t_name = luaT_typename(L,2);
  THByteTensor *b_t = (THByteTensor *)luaT_checkudata(L, 2, t_name);
	THArgCheck(b_t->nDimension == 1, 2, "Tensor must have one dimension.");
	THArgCheck(b_t->stride[0] == 1, 2, "Improper tensor memory layout (non-contiguous).");
	unsigned char* dest = b_t->storage->data;
  if( dest == NULL )
    return luaL_error(L, "Bad destination.");
	size_t type_sz;
	size_t byte_sz = b_t->size[0];
	char type = t_name[6];
  switch (type) {
  case 'B':
		type_sz = sizeof(unsigned char);
    break;
  case 'C':
		type_sz = sizeof(char);
    break;
  case 'S':
		type_sz = sizeof(short);
    break;
  case 'L':
		type_sz = sizeof(long);
    break;
  case 'I':
    type_sz = sizeof(int);
    break;
  case 'F':
    type_sz = sizeof(float);
    break;
  case 'D':
    type_sz = sizeof(double);
    break;
  default:
    return luaL_error(L, "Bad tensor type.");
  }
	byte_sz *= type_sz;
	dest += b_t->storageOffset * type_sz;
  size_t n_copy = luaL_optint( L, 3, n_str/type_sz ) * type_sz;
  size_t offset = luaL_optint(L,4,0) * type_sz;
	src += offset;
  if( n_copy > byte_sz || (n_copy+offset)>n_str ){
    return luaL_error(L,
				"Bad copy size.\nn_str: %d\nbyte_sz: %d\noffset: %d\nn_copy:%d",
				n_str,byte_sz,offset,n_copy);
	}
	memcpy( dest, src, n_copy );
  return 0;
}
static int lua_string2tensor ( lua_State * L ) { size_t n_str ; char * src = ( char * ) lua_tolstring ( L , 1 , & n_str ) ; if ( src == NULL ) return luaL_error ( L , " " ) ; const char * t_name = luaT_typename ( L , 2 ) ; THByteTensor * b_t = ( THByteTensor * ) luaT_checkudata ( L , 2 , t_name ) ; THArgCheck ( b_t -> nDimension == 1 , 2 , " " ) ; THArgCheck ( b_t -> stride [ 0 ] == 1 , 2 , " " ) ; unsigned char * dest = b_t -> storage -> data ; if ( dest == NULL ) return luaL_error ( L , " " ) ; size_t type_sz ; size_t byte_sz = b_t -> size [ 0 ] ; char type = t_name [ 6 ] ; switch ( type ) { case ' ' : type_sz = sizeof ( unsigned char ) ; break ; case ' ' : type_sz = sizeof ( char ) ; break ; case ' ' : type_sz = sizeof ( short ) ; break ; case ' ' : type_sz = sizeof ( long ) ; break ; case ' ' : type_sz = sizeof ( int ) ; break ; case ' ' : type_sz = sizeof ( float ) ; break ; case ' ' : type_sz = sizeof ( double ) ; break ; default : return luaL_error ( L , " " ) ; } byte_sz *= type_sz ; dest += b_t -> storageOffset * type_sz ; size_t n_copy = luaL_optint ( L , 3 , n_str / type_sz ) * type_sz ; size_t offset = luaL_optint ( L , 4 , 0 ) * type_sz ; src += offset ; if ( n_copy > byte_sz || ( n_copy + offset ) > n_str ) { return luaL_error ( L , " \n \n \n \n " , n_str , byte_sz , offset , n_copy ) ; } memcpy ( dest , src , n_copy ) ; return 0 ; }
Copies string data to the torch tensor
Copies string data to the torch tensor
/* Grab the source string */ /* Check that we have a src (should be true...) */ /* Grab the destination tensor */ /* Safety checks */ /* Find the destination pointer */ /* Size adjustment */ /* Find the byte size of the storage segment */ /* Add an offset */ /* Number of string elements to copy */ /* Optional offset (in elements) of the string */ /* Add the offset from copying the string */ /* Ensure that we are not overstepping our boundary of the tensor */ /* Copy the data */