The Battle for Wesnoth  1.15.3+dev
Classes | Typedefs | Functions
utf8 Namespace Reference

Functions for converting Unicode wide-char strings to UTF-8 encoded strings, back and forth. More...

Classes

class  invalid_utf8_exception
 Thrown by operations encountering invalid UTF-8 data. More...
 

Typedefs

typedef ucs4::iterator_base< std::string, ucs4_convert_impl::convert_impl< char >::type > iterator
 

Functions

static int byte_size_from_utf8_first (const unsigned char ch)
 
std::string lowercase (const std::string &s)
 Returns a lowercased version of the string. More...
 
std::size_t index (const std::string &str, const std::size_t index)
 Codepoint index corresponding to the nth character in a UTF-8 string. More...
 
std::size_t size (const std::string &str)
 Length in characters of a UTF-8 string. More...
 
std::string & insert (std::string &str, const std::size_t pos, const std::string &insert)
 Insert a UTF-8 string at the specified position. More...
 
std::string & erase (std::string &str, const std::size_t start, const std::size_t len=std::string::npos)
 Erases a portion of a UTF-8 string. More...
 
std::string & truncate (std::string &str, const std::size_t size)
 Truncates a UTF-8 string to the specified number of characters. More...
 
void truncate_as_ucs4 (std::string &str, const std::size_t size)
 Truncates a UTF-8 string to the specified number of characters. More...
 

Detailed Description

Functions for converting Unicode wide-char strings to UTF-8 encoded strings, back and forth.

Typedef Documentation

◆ iterator

Definition at line 40 of file unicode.hpp.

Function Documentation

◆ byte_size_from_utf8_first()

static int utf8::byte_size_from_utf8_first ( const unsigned char  ch)
static

Definition at line 36 of file unicode.cpp.

References count_leading_ones().

Referenced by index(), and size().

◆ erase()

std::string & utf8::erase ( std::string &  str,
const std::size_t  start,
const std::size_t  len = std::string::npos 
)

Erases a portion of a UTF-8 string.

Parameters
strUTF-8 encoded string.
startStart position.
lenNumber of characters to erase.
Note
This implementation does not check for valid UTF-8. Don't use it for user input.

Definition at line 104 of file unicode.cpp.

References index(), size(), and str.

Referenced by gui2::password_box::delete_selection(), gui2::text_box::delete_selection(), and truncate().

◆ index()

std::size_t utf8::index ( const std::string &  str,
const std::size_t  index 
)

Codepoint index corresponding to the nth character in a UTF-8 string.

Returns
str.length() if there are less than index characters.

Definition at line 71 of file unicode.cpp.

References byte_size_from_utf8_first(), ERR_GENERAL, i, and len.

Referenced by pathfind::a_star_search(), unit::ability_active(), display::action_pressed(), actions::actor_sighted(), gui2::tree_view_node::add_child(), simple_wml::node::add_child_at(), config::add_child_at(), config::add_child_at_total(), gui2::tree_view_node::add_child_impl(), gui2::tree_view_node::add_sibling(), simple_wml::node::apply_diff(), config::apply_diff(), simple_wml::node::attr(), BOOST_AUTO_TEST_CASE(), team::calculate_enemies(), team::calculate_is_enemy(), editor::editor_controller::can_execute_command(), play_controller::hotkey_handler::can_execute_command(), ai::cfun_ai_get_suitable_keep(), config::child(), wesnothd::server::cleanup_game(), config::clear_diff_track(), persist_file_context::clear_var(), gui2::chatbox::close_window_button_callback(), mariadb::result_set::column_name(), mariadb::result_set::column_size(), mariadb::result_set::column_type(), ng::connect_engine::connect_engine(), gui2::text_box_base::copy_selection(), gui2::generator< minimum_selection, maximum_selection, my_placement, select_action >::create_item(), gui2::dialogs::unit_attack::damage_calc_callback(), gui2::dialogs::game_load::delete_button_callback(), gui2::generator< minimum_selection, maximum_selection, my_placement, select_action >::delete_item(), gui2::dialogs::unit_recall::dismiss_unit(), editor::editor_controller::do_execute_command(), play_controller::hotkey_handler::do_execute_command(), display::enable_menu(), erase(), gui::menu::erase_item(), gui2::dialogs::mp_join_game::fetch_game_config(), font::pango_text::find_links(), pathfind::find_routes(), ng::flg_manager::find_suitable_faction(), t_translation::ter_map::get(), play_controller::hotkey_handler::get_action_image(), editor::editor_controller::get_action_state(), hotkey::command_executor::get_action_state(), tod_manager::get_area_by_index(), gui2::styled_widget::get_canvas(), gui2::tree_view_node::get_child_at(), font::pango_text::get_column_line(), tod_manager::get_current_area_time(), image::cache_type< T >::get_element(), gui2::generator< minimum_selection, maximum_selection, my_placement, select_action >::get_item_shown(), wb::side_actions::get_numbers(), gui2::generator< minimum_selection, maximum_selection, my_placement, select_action >::get_ordered_index(), gui2::tree_view::get_root_node(), team::get_side_color_id(), team::get_side_color_range(), tod_manager::get_time_of_day(), font::pango_text::get_token(), gui2::dialogs::addon_manager::get_type_filter_visibility(), editor::context_manager::gui(), hotkey::command_executor::handle_keyup(), editor::common_palette::handler_members(), gui2::dialogs::preferences_dialog::hotkey_filter_callback(), gui2::generator< minimum_selection, maximum_selection, my_placement, select_action >::impl_create_items(), gui2::generator< minimum_selection, maximum_selection, my_placement, select_action >::impl_draw_children(), insert(), replay_recorder_base::insert_command(), spirit_po::catalog< hashmap_type, pf_compiler >::insert_message(), font::pango_text::insert_text(), game_lua_kernel::intf_get_sides(), intf_music_add(), ng::create_engine::is_campaign(), team::is_enemy(), gui2::generator< minimum_selection, maximum_selection, my_placement, select_action >::item(), gui2::generator< minimum_selection, maximum_selection, my_placement, select_action >::item_ordered(), team::knows_about_team(), luaW_getglobal(), luaW_table_get_def(), wesnothd::make_add_diff(), wesnothd::make_change_diff(), wesnothd::make_delete_diff(), ng::depcheck::MAKE_ENUM(), terrain_filter::match_internal(), display::menu_pressed(), team::merge_shroud_map_data(), gui2::dialogs::addon_manager::on_order_changed(), variable_info_implementation::replace_range_h::operator()(), variable_info_implementation::merge_range_h::operator()(), recall_list_manager::operator[](), cave_map_generator::cave_map_generator_job::place_chamber(), gui2::pane::place_children(), gui2::pane::place_or_set_origin_children(), ai::readonly_context_impl::power_projection(), gui2::dialogs::mp_create_game::pre_show(), turn_info::process_network_data(), wesnothd::game::process_turn(), ai::push_move_map(), metrics::record_sample(), simple_wml::node::remove_child(), config::remove_child(), config::remove_children(), replay_recorder_base::remove_command(), ai::ai_default_rca::candidate_action_evaluation_loop::remove_completed_cas(), wesnothd::server::remove_player(), hotkey::remove_wml_hotkey(), gui2::dialogs::unit_recall::rename_unit(), play_controller::hotkey_handler::replay_skip_animation(), playsingle_controller::hotkey_handler::reset_replay(), editor::editor_controller::save_map(), gui2::generator< minimum_selection, maximum_selection, my_placement, select_action >::select_item(), editor::map_context::set_active_area(), ng::create_engine::set_current_era_index(), ng::flg_manager::set_current_faction(), ng::flg_manager::set_current_gender(), ng::flg_manager::set_current_leader(), gui2::dialogs::editor_resize_map::set_direction_icon(), editor::editor_palette< overlay >::set_group(), ng::side_engine::set_index(), gui2::generator< minimum_selection, maximum_selection, my_placement, select_action >::set_item_selected(), gui2::generator< minimum_selection, maximum_selection, my_placement, select_action >::set_item_shown(), gui2::pane::set_origin_children(), gui2::dialogs::theme_list::set_selected_index(), gui2::dialogs::simple_item_selector::set_selected_index(), editor::location_palette::set_start_item(), editor::editor_palette< overlay >::set_start_item(), replay_controller::should_stop(), attack_type::special_active_impl(), ng::connect_engine::start_game(), editor::editor_palette< overlay >::start_num(), replay_recorder_base::swap(), editor::context_manager::switch_context(), TEST_F(), tod_manager::times(), theme::action::title(), gui2::generator_base::toggle_item(), theme::action::tooltip(), gui2::dialogs::mp_create_game::update_games_list(), and game_events::WML_HANDLER_FUNCTION().

◆ insert()

std::string & utf8::insert ( std::string &  str,
const std::size_t  pos,
const std::string &  insert 
)

Insert a UTF-8 string at the specified position.

Definition at line 99 of file unicode.cpp.

References index().

Referenced by gui2::text_box_base::handle_editing(), gui2::password_box::insert_char(), and font::pango_text::insert_text().

◆ lowercase()

std::string utf8::lowercase ( const std::string &  s)

◆ size()

std::size_t utf8::size ( const std::string &  str)

◆ truncate()

std::string & utf8::truncate ( std::string &  str,
const std::size_t  size 
)

Truncates a UTF-8 string to the specified number of characters.

Parameters
strUTF-8 encoded string.
sizeSize to truncate to.
Note
This implementation does not check for valid UTF-8. Don't use it for user input.

Definition at line 117 of file unicode.cpp.

References erase().

Referenced by BOOST_AUTO_TEST_CASE(), utils::ellipsis_truncate(), and font::pango_text::set_maximum_length().

◆ truncate_as_ucs4()

void utf8::truncate_as_ucs4 ( std::string &  str,
const std::size_t  size 
)

Truncates a UTF-8 string to the specified number of characters.

If the string has more than size UTF-8 characters it will be truncated to this size.

The output is guaranteed to be valid UTF-8.

Parameters
[in,out]str[in] String encoded in UTF-8. [out] String encoded UTF-8 that contains at most size codepoints.
sizeThe size to truncate to.

Definition at line 122 of file unicode.cpp.

References size(), str, mariadb::value::string, and unicode_cast().

Referenced by wesnothd::chat_message::truncate_message().