Function egg_mode::text::character_count [] [src]

pub fn character_count(
    text: &str,
    http_url_len: i32,
    https_url_len: i32
) -> (usize, bool)

Returns how many characters the given text would be, after accounting for URL shortening. Also returns an indicator of whether the given text is a valid length for a tweet.

For the http_url_len and https_url_len parameters, call service::config and use the short_url_len and short_url_len_https fields on the struct that's returned. If you want to perform these checks offline, twitter-text's sample code and tests assume 23 characters for both sizes. At the time of this writing (2016-11-28), those numbers were also being returned from the service itself.

Examples

use egg_mode::text::character_count;

 let (count, _) = character_count("This is a test.", 23, 23);
 assert_eq!(count, 15);

 // URLs get replaced by a t.co URL of the given length
 let (count, _) = character_count("test.com", 23, 23);
 assert_eq!(count, 23);

 // Multiple URLs get shortened individually
 let (count, _) =
     character_count("Test https://test.com test https://test.com test.com test", 23, 23);
 assert_eq!(count, 86);