1. Need a hand with a small C conversion

G'day everyone,

Seeing as Euphoria doesn't have much in the way of bit shifting (well, as far as I know, anyway), could someone advise me on the best way to convert this bit of code into Euphoria?

    UTF32 X = (hi & ((1<<6) -1)) << 10 | lo & ((1<<10) -1); 
    UTF32 W = (hi >> 6) & ((1<<5) - 1); 
    UTF32 U = W + 1 ; 
    codepoint = U << 16 | X; 

The UTF32 datatype is defined as typedef unsigned __int32 UTF32; and hi and lo as UTF16 which is typedef unsigned __int16 UTF16;

The algorithms are from the Unicode Consortium http://www.unicode.org/unicode/faq/utf_bom.html#35

Kind regards,

Bruce.

new topic     » topic index » view message » categorize

2. Re: Need a hand with a small C conversion

try these..

positive shift left

negative shift right global function bitshift(atom x, atom shift) return floor(x * power(2,shift) end function

global function lshift(atom x, atom shift) return bitshift(x,shift) end function

global function rshift(atom x, atom shift) return bitshift(x,-shift) end function

new topic     » goto parent     » topic index » view message » categorize

3. Re: Need a hand with a small C conversion

here is some functions for splitting ints into hi and lo.


global function lo_word( atom a )


syntax: i = lo_word( a )

description: returns the low word(2 bytes) i, from the long(4 bytes) a

return low word return and_bits( a, #FFFF ) end function


global function hi_word( atom a )


syntax: i = hi_word( a )

description: returns the high word(2 bytes) i, from the long(4 bytes) a

return high word return and_bits(floor( a / #10000 ), #FFFF) end function

new topic     » goto parent     » topic index » view message » categorize

4. Re: Need a hand with a small C conversion

sorry, forgot to format.

-- positive shift left  
-- negative shift right 
global function bitshift(atom x, atom shift) 
  return floor(x * power(2,shift) 
end function  
 
global function lshift(atom x, atom shift) 
  return bitshift(x,shift) 
end function  
 
global function rshift(atom x, atom shift) 
  return bitshift(x,-shift) 
end function  
 
------------------------------------------------ 
global function lo_word( atom a ) 
------------------------------------------------ 
-- syntax: i = lo_word( a ) 
-- description: 
--    returns the low word(2 bytes) i, from the long(4 bytes) a 
 
    -- return low word 
    return and_bits( a, #FFFF ) 
end function 
 
------------------------------------------------ 
global function hi_word( atom a ) 
------------------------------------------------ 
-- syntax: i =  hi_word( a ) 
-- description: 
--    returns the high word(2 bytes) i, from the long(4 bytes) a 
 
    -- return high word 
    return and_bits(floor( a / #10000 ), #FFFF) 
end function 
new topic     » goto parent     » topic index » view message » categorize

5. Re: Need a hand with a small C conversion

Thanks, scooby! Hugely helpful.

new topic     » goto parent     » topic index » view message » categorize

6. Re: Need a hand with a small C conversion

Here's the completed routine, in case any one's interested:

function MergeSurrogates( atom hi, atom lo ) 
  integer X 
  integer W 
  integer U 
  X = or_bits( lshift( and_bits( hi, lshift(1,6) - 1), 10 ), and_bits( lo, lshift( 1, 10 ) - 1 ) ) 
  W = and_bits( rshift( hi, 6 ), lshift( 1, 5 ) - 1) 
  U = W + 1 
  return or_bits( lshift( U, 16 ), X ) 
end function 

Thank to all contributors!

Bruce.

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu