The Battle for Wesnoth  1.15.0-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(), and size().

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, and i.

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(), 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_type_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(), luaW_tableget(), 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(), ng::connect_engine::start_game(), editor::editor_palette< overlay >::start_num(), replay_recorder_base::swap(), editor::context_manager::switch_context(), 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 
)

◆ lowercase()

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

◆ size()

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

Length in characters of a UTF-8 string.

Definition at line 86 of file unicode.cpp.

References byte_size_from_utf8_first(), ERR_GENERAL, and i.

Referenced by wb::action::action(), gui2::gui_definition::activate(), gui2::scrollbar_base::all_items_visible(), plugins_manager::any_running(), gui2::slider_base::at_end(), wb::attack::attack(), BF_decode(), BF_encode(), BOOST_AUTO_TEST_CASE(), gui2::implementation::builder_drawing::build(), gui2::spacer::calculate_best_size(), gui2::size_lock::calculate_best_size(), gui2::matrix::calculate_best_size(), gui2::grid::calculate_best_size(), team::calculate_enemies(), font::pango_text::calculate_size(), editor::editor_controller::can_execute_command(), gui2::grid::can_mouse_focus(), gui2::widget::can_mouse_focus(), ai::cfun_ai_get_recruitment_pattern(), events::menu_handler::change_side(), team::shroud_map::clear(), gui2::text_box::clear(), gui2::grid_implementation::column_request_reduce_width(), gui2::scrollbar_container::content_resize_request(), utils::basic_string_view< charT, traits >::copy(), gui2::dialogs::chat_log::model::count_of_pages(), gui2::policy::placement::independent::create_item(), cut_surface(), wfl::builtins::DEFINE_WFL_FUNCTION(), editor::editor_controller::do_execute_command(), do_replay_handle(), editor::location_palette::draw_contents(), editor::editor_palette< overlay >::draw_contents(), wb::draw_numbers(), editor::editor_display::draw_sidebar(), font::draw_text(), DumpString(), erase(), combatant::fight(), filesystem::file_size(), gui2::scrollbar_container::finalize_subclass(), utils::basic_string_view< charT, traits >::find(), wfl::formula_function_expression::formula_function_expression(), game_launcher::game_launcher(), gui2::styled_widget::get_best_text_size(), wfl::function_symbol_table::get_builtins(), variable_info_implementation::get_child_range(), gui2::text_box_base::get_composition_length(), gui2::tree_view_node::get_current_size(), filesystem::get_files_in_dir(), gui2::tree_view_node::get_folded_size(), font::sdl_ttf::get_font(), plugins_manager::get_kernel_base(), gui2::size_lock::get_state(), get_system_username(), gui2::tree_view_node::get_unfolded_size(), growstack(), gui2::text_box_base::handle_editing(), campaignd::server::handle_request_campaign(), filesystem::ifs_read(), impl_unit_attacks_len(), display::init_flags_for_side_internal(), gui2::password_box::insert_char(), gui2::text_box_base::insert_char(), font::pango_text::insert_text(), intf_default_generate_height_map(), intf_describe_plugins(), game_lua_kernel::intf_find_cost_map(), game_lua_kernel::intf_find_path(), game_lua_kernel::intf_find_reach(), game_lua_kernel::intf_get_fog_or_shroud(), game_lua_kernel::intf_get_side_variable(), game_lua_kernel::intf_get_starting_location(), game_lua_kernel::intf_is_enemy(), game_lua_kernel::intf_match_side(), game_lua_kernel::intf_print(), game_lua_kernel::intf_put_recall_unit(), lua_fileops::intf_read_file(), game_lua_kernel::intf_set_side_variable(), spirit_po::po_message::is_plural(), gui2::window_implementation::layout(), gui2::grid::layout(), gui2::window::layout(), gui2::window::layout_linked_widgets(), game_config::library_name(), LoadString(), luaD_growstack(), luaL_loadbufferx(), luaL_tolstring(), luaS_resize(), luaZ_fill(), hotkey::hotkey_keyboard::matches_helper(), map_location::matches_range(), plugins_manager::notify_event(), filesystem::ofs_write(), font::open_font_impl(), utils::operator<<(), packint(), php_crypt_blowfish_rn(), team::shroud_map::place(), gui2::policy::placement::table::place(), plugins_manager::play_slice(), gui2::dialogs::title_screen::pre_show(), turn_info::process_network_data(), propagatemark(), ucs4_convert_impl::utf8_impl::read(), gui2::container_base::reduce_height(), gui2::grid::reduce_height(), gui2::container_base::reduce_width(), gui2::grid::reduce_width(), gui2::grid::request_placement(), gui2::container_base::request_reduce_height(), gui2::scrollbar_container::request_reduce_height(), gui2::styled_widget::request_reduce_height(), gui2::policy::placement::horizontal_list::request_reduce_height(), gui2::grid::request_reduce_height(), gui2::policy::placement::vertical_list::request_reduce_height(), gui2::container_base::request_reduce_width(), gui2::scrollbar_container::request_reduce_width(), gui2::styled_widget::request_reduce_width(), gui2::grid::request_reduce_width(), font::text_cache::resize(), gui2::tree_view::resize_content(), gui2::listbox::resize_content(), editor::map_fragment::rotate_60_ccw(), editor::map_fragment::rotate_60_cw(), gui2::grid_implementation::row_request_reduce_height(), preferences::save_sound_buffer_size(), wb::manager::save_temp_move(), CVideo::screen_area(), gui2::listbox::select_last_row(), wb::manager::send_network_data(), CVideo::set_help_string(), gui2::dialogs::file_dialog::set_input_text(), editor::mouse_action_item::set_item_mouse_overlay(), gui2::widget::set_layout_size(), editor::mouse_action_map_label::set_mouse_overlay(), editor::mouse_action_village::set_mouse_overlay(), editor::mouse_action_select::set_mouse_overlay(), editor::mouse_action_paste::set_mouse_overlay(), editor::mouse_action_starting_position::set_mouse_overlay(), display::set_playing_team(), display::set_team(), editor::mouse_action::set_terrain_mouse_overlay(), editor::mouse_action_unit::set_unit_mouse_overlay(), gui2::password_box::set_value(), setarrayvector(), campaign_controller::show_carryover_message(), help::show_help(), utils::signed_percent(), special_locations_len(), str_pack(), str_packsize(), str_unpack(), t_translation::string_to_layer_(), utils::basic_string_view< charT, traits >::substr(), utils::detail::sv_insert_aligned(), tremove(), truncate_as_ucs4(), unpackint(), gui2::dialogs::game_cache_options::update_cache_size_display(), gui2::dialogs::chat_log::controller::update_view_from_model(), validate_side(), team::shroud_map::value(), game_events::WML_HANDLER_FUNCTION(), font::word_wrap_text(), and unit::xp_color().

◆ 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(), and unicode_cast().

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