This Question is Answered

15 "helpful" answers available (3 pts)
10 Replies Last post: Jul 30, 2008 6:49 AM by cbarber

Is there any accessor like max-chars, but it evaluate the byte count?

Jul 24, 2008 11:55 PM

Click to view markecho's profile Level 2 markecho 35 posts since
Oct 17, 2007

For example:

After I set max-chars = 6 of the TextField, I could input restricted six half-width chars "abcdef" ,

but , I also could input restricted six full-width chars "あなたわたし",

That's not what I want. I want to input restricted three full-width chars "あなた"

How could I do to restrict the maximum bytes in the TextField?

Thank you.

Click to view URPradhan's profile Level 7 URPradhan 141 posts since
Mar 6, 2008
1. Re: Is there any accessor like max-chars, but it evaluate the byte count? Jul 25, 2008 1:26 AM
Half-width/full-width chars ??? What are they ?
Click to view carl's profile Curl carl 74 posts since
Oct 17, 2007
2. Re: Is there any accessor like max-chars, but it evaluate the byte count? Jul 25, 2008 3:26 AM
in response to: URPradhan
I believe the original post may be referring to: http://everything2.com/index.pl?node=Halfwidth%20and%20Fullwidth%20Forms
Click to view Kamal's profile Curl Kamal 139 posts since
Oct 17, 2007
3. Re: Is there any accessor like max-chars, but it evaluate the byte count? Jul 25, 2008 6:06 AM
You cannot specify number of bytes allowed for a TextField. In fact what it would mean anyway? The input to a TextField is characters. A character in Curl could be one to four bytes, depending on the character. When you set max-chars = 6, it means that the TextField will allow only 6 characters to be entered. It is immaterial how many bytes a character takes. Iy you want only three full-width characters to be allowed as an input to a TextField you can set the max-char size to 3 or the TextField. Also if you want your TextField to initially start with full-width mode then you can set input-method-keyboard-mode option of the TextField. See InputMethodKeyboardMode for various modes you can set the text field to. Setting input-method-keyboard-mode currently only works on Microsoft Windows and is a no op in other supported platforms.
Click to view phil's profile Curl phil 33 posts since
Oct 17, 2007
4. Re: Is there any accessor like max-chars, but it evaluate the byte count? Jul 28, 2008 9:53 AM
in response to: Kamal
To add to what Kamal said, a 'char' in curl is a 32 bit unicode value. For efficiency purposes, a char may take up 8 or 16 bits in memory, but it's best to think of them as always being 32 bits.

When people talk about single or double byte characters, they're talking about the representation of a character in an encoding. Depending on the encoding, a character can be represented as a different number of bytes. So unless you're talking about a character's representation in an encoding, there's no such thing as a single or double byte character.

If you're trying to limit the field contents based on how much room they will take up once they're encoded (in a particular CharEncoding), you can accomplish this by using a custom validation proc for your text field, which encodes the user input as the user types. Although I'm not sure if that's what you're really trying to do here.
Click to view markecho's profile Level 2 markecho 35 posts since
Oct 17, 2007
5. Re: Is there any accessor like max-chars, but it evaluate the byte count? Jul 29, 2008 8:06 PM

thanks all of you,

Sorry , I'm afraid I haven't described the problem clearly yet ,

I mean I have a TextField, and I want to restrict the number of the content that is input in the TextField.

For example:

I set some property --- RestrictNum = 6,

(1) I input SBC case character --- 123456, and only 123 is displayed in the TextField;

(2) I input DBC case character --- 123456, and 123456 is displayed in the TextField;

(Note that the 123456's length is twice as long as 123456's length)

How should I do to implement the function above? Thank you again.


Click to view carl's profile Curl carl 74 posts since
Oct 17, 2007
6. Re: Is there any accessor like max-chars, but it evaluate the byte count? Jul 29, 2008 10:35 PM
in response to: markecho
It's certainly possible, but you will have to determine exactly what encodings you're targeting. The encode-characters proc and method would probably be of use to you here. You could convert the String value of the TextField to a ByteVec and see how long that is. I recommend looking at the documentation for encode-characters and CharEncoding. There's an example in "Encoding Strings of Characters" in the documentation chapter "Reading and Writing Text Files".

For the best implementation, you would want to override the TextFieldUI you were using, probably just to check every time an insertable KeyPress happens, and possibly even the TextField itself. You could avoid some of that work if you were willing to just truncate the value in the TextField on an overflow, or something similar.

As a quick example, the following writes out the length in bytes for the given encoding on each ValueChanged. The target encoding is utf8 here, though you might be looking for "shift-jis" or "euc-jp".

{TextField
    {on ValueChanged at tf:TextField do
        def in = tf.value
        let unicodeout:ByteVec =
            {ByteVec
                max-size = {value in.size} *
                CharEncoding.utf8.transcode-max-expansion-factor
            }
        || Use get-character-encoding-by-name procedure
        let encoding:CharEncoding =
            {get-character-encoding-by-name  "utf8"}
        let unicodecount:int =
            {encode-characters in, unicodeout, encoding}
        {dump unicodeout.size}
    }
}
Click to view Duke's profile Curl Duke 154 posts since
Oct 17, 2007
7. Re: Is there any accessor like max-chars, but it evaluate the byte count? Jul 29, 2008 10:37 PM
in response to: markecho
You might be able to use the proc, {char-fold-width ch:char}:char , to detect which characters are half width based on whether or not the input char == the char value returned. Then you could make your own validator MyWidthValidator to use, as in {TextField {validate-with {MyWidthValidator}}}.
Click to view markecho's profile Level 2 markecho 35 posts since
Oct 17, 2007
8. Re: Is there any accessor like max-chars, but it evaluate the byte count? Jul 29, 2008 11:51 PM
in response to: carl

unicodeout.size?

what about {CString in}.byte-size?

Click to view carl's profile Curl carl 74 posts since
Oct 17, 2007
9. Re: Is there any accessor like max-chars, but it evaluate the byte count? Jul 30, 2008 12:08 AM
in response to: markecho
CString defaults to the host encoding, so I think you would want to use CharEncoding instead for better control. Also CString 's not really intended for that purpose.
Click to view cbarber's profile Curl cbarber 115 posts since
Sep 27, 2007
10. Re: Is there any accessor like max-chars, but it evaluate the byte count? Jul 30, 2008 6:49 AM
in response to: phil
Note that the 'char' data type always takes 32 bits. However, some data structures containing chars, can use more compact representations. String and StringBuf both internally contain an array of char8, char16 or char based on the widest character value in the string. Text streams store characters in bytes encoded according to the specified CharEncoding.