dbTalk Databases Forums  

substrb and MBCS

comp.databases.oracle.misc comp.databases.oracle.misc


Discuss substrb and MBCS in the comp.databases.oracle.misc forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
michael.young@paetec.com
 
Posts: n/a

Default substrb and MBCS - 02-04-2008 , 04:48 PM






If one uses substrb on a MBCS string, will the returned result
possibly include a "partial character" (i.e., only the leading byte or
bytes of a multiple byte character), or does the function "trim" to
the nearest whole character?

According to the documentation for RAISE_APPLICATION_ERROR, this
function only takes 2048 *bytes* for an application defined error
message. If I want to ensure that I don't exceed this, I'll simply
substrb( message, 1, 2048 ), but the problem is that if substrb will
split characters, then my resultant string may end with a different
(or invalid) character code point (depending on my character set).

The only way I know to avoid this is to incrementally build the
message string in a loop, character by character, and then test the
length of the string after adding each character - the desired
substring is obtained by either taking the entire source string (if
the source string is exhausted before exceeding the 2048 byte
threshold), or, once the 2048 byte threshold is exceeded in the
destination string, removing the last character from that string.
Since 32-bit characters are the longest character encodings (AFAIK),
then I could optimize this slightly by starting with the substr( msg,
1, 512 ) as my initial destination string, and then build/test the
string from there. Doable, yes, but UGLY!

Does anyone have any other ideas to deal with this?

TIA,
Mike

Reply With Quote
  #2  
Old   
michael.young@paetec.com
 
Posts: n/a

Default Re: substrb and MBCS - 02-04-2008 , 08:05 PM






After a little more thought, I've changed a few details - just for
efficiency - see the example code below (not tested)... however, this
is not an optimal solution; I'd definitely prefer a definitive way to
avoid splitting a character without all this extra effort...
If anyone has a better way, or knows more about the behavior of
substrb, I'd greatly appreciate any input.


v_max_text_byte_length integer := 2048 ;

MAX_CHARACTER_BYTE_LENGTH constant integer := 4 ;

diff := trunc( ( ( lengthb( src_msg ) - v_max_text_byte_length ) /
MAX_CHARACTER_BYTE_LENGTH ), 0 ) ;

if ( diff < 0 ) then

tgt_msg := src_msg ;

else

tgt_msg := substr( src_msg, 1, src_len - diff ) ;

tgt_len_char := length( tgt_msg ) ;

loop

tgt_len := lengthb( tgt_msg ) ;
exit when ( tgt_len <= 2048 ) ;
tgt_len_char := tgt_len_char - 1 ;
tgt_msg := substr( tgt_msg, 1, tgt_len_char ) ;

end loop ;

end if ;

Reply With Quote
  #3  
Old   
michael.young@paetec.com
 
Posts: n/a

Default Re: substrb and MBCS - 02-04-2008 , 08:05 PM



After a little more thought, I've changed a few details - just for
efficiency - see the example code below (not tested)... however, this
is not an optimal solution; I'd definitely prefer a definitive way to
avoid splitting a character without all this extra effort...
If anyone has a better way, or knows more about the behavior of
substrb, I'd greatly appreciate any input.


v_max_text_byte_length integer := 2048 ;

MAX_CHARACTER_BYTE_LENGTH constant integer := 4 ;

diff := trunc( ( ( lengthb( src_msg ) - v_max_text_byte_length ) /
MAX_CHARACTER_BYTE_LENGTH ), 0 ) ;

if ( diff < 0 ) then

tgt_msg := src_msg ;

else

tgt_msg := substr( src_msg, 1, src_len - diff ) ;

tgt_len_char := length( tgt_msg ) ;

loop

tgt_len := lengthb( tgt_msg ) ;
exit when ( tgt_len <= 2048 ) ;
tgt_len_char := tgt_len_char - 1 ;
tgt_msg := substr( tgt_msg, 1, tgt_len_char ) ;

end loop ;

end if ;

Reply With Quote
  #4  
Old   
michael.young@paetec.com
 
Posts: n/a

Default Re: substrb and MBCS - 02-04-2008 , 08:05 PM



After a little more thought, I've changed a few details - just for
efficiency - see the example code below (not tested)... however, this
is not an optimal solution; I'd definitely prefer a definitive way to
avoid splitting a character without all this extra effort...
If anyone has a better way, or knows more about the behavior of
substrb, I'd greatly appreciate any input.


v_max_text_byte_length integer := 2048 ;

MAX_CHARACTER_BYTE_LENGTH constant integer := 4 ;

diff := trunc( ( ( lengthb( src_msg ) - v_max_text_byte_length ) /
MAX_CHARACTER_BYTE_LENGTH ), 0 ) ;

if ( diff < 0 ) then

tgt_msg := src_msg ;

else

tgt_msg := substr( src_msg, 1, src_len - diff ) ;

tgt_len_char := length( tgt_msg ) ;

loop

tgt_len := lengthb( tgt_msg ) ;
exit when ( tgt_len <= 2048 ) ;
tgt_len_char := tgt_len_char - 1 ;
tgt_msg := substr( tgt_msg, 1, tgt_len_char ) ;

end loop ;

end if ;

Reply With Quote
  #5  
Old   
michael.young@paetec.com
 
Posts: n/a

Default Re: substrb and MBCS - 02-04-2008 , 08:05 PM



After a little more thought, I've changed a few details - just for
efficiency - see the example code below (not tested)... however, this
is not an optimal solution; I'd definitely prefer a definitive way to
avoid splitting a character without all this extra effort...
If anyone has a better way, or knows more about the behavior of
substrb, I'd greatly appreciate any input.


v_max_text_byte_length integer := 2048 ;

MAX_CHARACTER_BYTE_LENGTH constant integer := 4 ;

diff := trunc( ( ( lengthb( src_msg ) - v_max_text_byte_length ) /
MAX_CHARACTER_BYTE_LENGTH ), 0 ) ;

if ( diff < 0 ) then

tgt_msg := src_msg ;

else

tgt_msg := substr( src_msg, 1, src_len - diff ) ;

tgt_len_char := length( tgt_msg ) ;

loop

tgt_len := lengthb( tgt_msg ) ;
exit when ( tgt_len <= 2048 ) ;
tgt_len_char := tgt_len_char - 1 ;
tgt_msg := substr( tgt_msg, 1, tgt_len_char ) ;

end loop ;

end if ;

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.