Skip to content

Commit c42b42c

Browse files
Use string_view for message object ctors + setters (#1178)
2 parents 46ae489 + 75913db commit c42b42c

File tree

2 files changed

+92
-79
lines changed

2 files changed

+92
-79
lines changed

include/dpp/message.h

+40-40
Original file line numberDiff line numberDiff line change
@@ -280,31 +280,31 @@ struct DPP_EXPORT select_option : public json_interface<select_option> {
280280
* @param value Value of option
281281
* @param description Description of option
282282
*/
283-
select_option(const std::string &label, const std::string &value, const std::string &description = "");
283+
select_option(std::string_view label, std::string_view value, std::string_view description = "");
284284

285285
/**
286286
* @brief Set the label
287287
*
288288
* @param l the user-facing name of the option. It will be truncated to the maximum length of 100 UTF-8 characters.
289289
* @return select_option& reference to self for chaining
290290
*/
291-
select_option& set_label(const std::string &l);
291+
select_option& set_label(std::string_view l);
292292

293293
/**
294294
* @brief Set the value
295295
*
296296
* @param v value to set. It will be truncated to the maximum length of 100 UTF-8 characters.
297297
* @return select_option& reference to self for chaining
298298
*/
299-
select_option& set_value(const std::string &v);
299+
select_option& set_value(std::string_view v);
300300

301301
/**
302302
* @brief Set the description
303303
*
304304
* @param d description to set. It will be truncated to the maximum length of 100 UTF-8 characters.
305305
* @return select_option& reference to self for chaining
306306
*/
307-
select_option& set_description(const std::string &d);
307+
select_option& set_description(std::string_view d);
308308

309309
/**
310310
* @brief Set the emoji
@@ -314,7 +314,7 @@ struct DPP_EXPORT select_option : public json_interface<select_option> {
314314
* @param animated true if animated emoji
315315
* @return select_option& reference to self for chaining
316316
*/
317-
select_option& set_emoji(const std::string &n, dpp::snowflake id = 0, bool animated = false);
317+
select_option& set_emoji(std::string_view n, dpp::snowflake id = 0, bool animated = false);
318318

319319
/**
320320
* @brief Set the option as default
@@ -529,7 +529,7 @@ class DPP_EXPORT component : public json_interface<component> {
529529
* @param label Label text to set. It will be truncated to the maximum length of 80 UTF-8 characters.
530530
* @return component& Reference to self
531531
*/
532-
component& set_label(const std::string &label);
532+
component& set_label(std::string_view label);
533533

534534
/**
535535
* @brief Set the default value of the text input component.
@@ -539,7 +539,7 @@ class DPP_EXPORT component : public json_interface<component> {
539539
* @param val Value text to set. It will be truncated to the maximum length of 4000 UTF-8 characters.
540540
* @return component& Reference to self
541541
*/
542-
component& set_default_value(const std::string &val);
542+
component& set_default_value(std::string_view val);
543543

544544
/**
545545
* @brief Set the url for dpp::cos_link types.
@@ -549,7 +549,7 @@ class DPP_EXPORT component : public json_interface<component> {
549549
* @param url URL to set. It will be truncated to the maximum length of 512 UTF-8 characters.
550550
* @return component& reference to self.
551551
*/
552-
component& set_url(const std::string &url);
552+
component& set_url(std::string_view url);
553553

554554
/**
555555
* @brief Set the style of the component, e.g. button colour.
@@ -572,7 +572,7 @@ class DPP_EXPORT component : public json_interface<component> {
572572
* If your Custom ID is longer than this, it will be truncated.
573573
* @return component& Reference to self
574574
*/
575-
component& set_id(const std::string &id);
575+
component& set_id(std::string_view id);
576576

577577
/**
578578
* @brief Set the component to disabled.
@@ -600,7 +600,7 @@ class DPP_EXPORT component : public json_interface<component> {
600600
* characters for modals.
601601
* @return component& Reference to self
602602
*/
603-
component& set_placeholder(const std::string &placeholder);
603+
component& set_placeholder(std::string_view placeholder);
604604

605605
/**
606606
* @brief Set the minimum number of items that must be chosen for a select menu
@@ -677,7 +677,7 @@ class DPP_EXPORT component : public json_interface<component> {
677677
* @param animated True if the custom emoji is animated.
678678
* @return component& Reference to self
679679
*/
680-
component& set_emoji(const std::string& name, dpp::snowflake id = 0, bool animated = false);
680+
component& set_emoji(std::string_view name, dpp::snowflake id = 0, bool animated = false);
681681
};
682682

683683
/**
@@ -706,21 +706,21 @@ struct DPP_EXPORT embed_footer {
706706
* @param t string to set as footer text. It will be truncated to the maximum length of 2048 UTF-8 characters.
707707
* @return A reference to self so this method may be "chained".
708708
*/
709-
embed_footer& set_text(const std::string& t);
709+
embed_footer& set_text(std::string_view t);
710710

711711
/**
712712
* @brief Set footer's icon url.
713713
* @param i url to set as footer icon url
714714
* @return A reference to self so this method may be "chained".
715715
*/
716-
embed_footer& set_icon(const std::string& i);
716+
embed_footer& set_icon(std::string_view i);
717717

718718
/**
719719
* @brief Set footer's proxied icon url.
720720
* @param p url to set as footer proxied icon url
721721
* @return A reference to self so this method may be "chained".
722722
*/
723-
embed_footer& set_proxy(const std::string& p);
723+
embed_footer& set_proxy(std::string_view p);
724724
};
725725

726726
/**
@@ -908,14 +908,14 @@ struct DPP_EXPORT embed {
908908
* @param text The text of the title. It will be truncated to the maximum length of 256 UTF-8 characters.
909909
* @return A reference to self so this method may be "chained".
910910
*/
911-
embed& set_title(const std::string &text);
911+
embed& set_title(std::string_view text);
912912

913913
/**
914914
* @brief Set embed description.
915915
* @param text The text of the title. It will be truncated to the maximum length of 4096 UTF-8 characters.
916916
* @return A reference to self so this method may be "chained".
917917
*/
918-
embed& set_description(const std::string &text);
918+
embed& set_description(std::string_view text);
919919

920920
/**
921921
* @brief Set the footer of the embed.
@@ -930,7 +930,7 @@ struct DPP_EXPORT embed {
930930
* @param icon_url an url to set as footer icon url (only supports http(s) and attachments)
931931
* @return A reference to self so this method may be "chained".
932932
*/
933-
embed& set_footer(const std::string& text, const std::string& icon_url);
933+
embed& set_footer(std::string_view text, std::string_view icon_url);
934934

935935
/**
936936
* @brief Set embed colour.
@@ -958,7 +958,7 @@ struct DPP_EXPORT embed {
958958
* @param url the url of the embed
959959
* @return A reference to self so this method may be "chained".
960960
*/
961-
embed& set_url(const std::string &url);
961+
embed& set_url(std::string_view url);
962962

963963
/**
964964
* @brief Add an embed field.
@@ -967,7 +967,7 @@ struct DPP_EXPORT embed {
967967
* @param is_inline Whether or not to display the field 'inline' or on its own line
968968
* @return A reference to self so this method may be "chained".
969969
*/
970-
embed& add_field(const std::string& name, const std::string &value, bool is_inline = false);
970+
embed& add_field(std::string_view name, std::string_view value, bool is_inline = false);
971971

972972
/**
973973
* @brief Set embed author.
@@ -983,36 +983,36 @@ struct DPP_EXPORT embed {
983983
* @param icon_url The icon URL of the author (only supports http(s) and attachments)
984984
* @return A reference to self so this method may be "chained".
985985
*/
986-
embed& set_author(const std::string& name, const std::string& url, const std::string& icon_url);
986+
embed& set_author(std::string_view name, std::string_view url, std::string_view icon_url);
987987

988988
/**
989989
* @brief Set embed provider.
990990
* @param name The provider name. It will be truncated to the maximum length of 256 UTF-8 characters.
991991
* @param url The provider url
992992
* @return A reference to self so this method may be "chained".
993993
*/
994-
embed& set_provider(const std::string& name, const std::string& url);
994+
embed& set_provider(std::string_view name, std::string_view url);
995995

996996
/**
997997
* @brief Set embed image.
998998
* @param url The embed image URL (only supports http(s) and attachments)
999999
* @return A reference to self so this method may be "chained".
10001000
*/
1001-
embed& set_image(const std::string& url);
1001+
embed& set_image(std::string_view url);
10021002

10031003
/**
10041004
* @brief Set embed video.
10051005
* @param url The embed video url
10061006
* @return A reference to self so this method may be "chained".
10071007
*/
1008-
embed& set_video(const std::string& url);
1008+
embed& set_video(std::string_view url);
10091009

10101010
/**
10111011
* @brief Set embed thumbnail.
10121012
* @param url The embed thumbnail url (only supports http(s) and attachments)
10131013
* @return A reference to self so this method may be "chained".
10141014
*/
1015-
embed& set_thumbnail(const std::string& url);
1015+
embed& set_thumbnail(std::string_view url);
10161016
};
10171017

10181018
/**
@@ -1360,15 +1360,15 @@ struct DPP_EXPORT sticker : public managed, public json_interface<sticker> {
13601360
* @param fn filename
13611361
* @return message& reference to self
13621362
*/
1363-
sticker& set_filename(const std::string &fn);
1363+
sticker& set_filename(std::string_view fn);
13641364

13651365
/**
13661366
* @brief Set the file content
1367-
*
1368-
* @param fc raw file content contained in std::string
1367+
*
1368+
* @param fc raw file content
13691369
* @return message& reference to self
13701370
*/
1371-
sticker& set_file_content(const std::string &fc);
1371+
sticker& set_file_content(std::string_view fc);
13721372

13731373
};
13741374

@@ -1575,7 +1575,7 @@ struct DPP_EXPORT poll {
15751575
* @param text Text for the question
15761576
* @return self for method chaining
15771577
*/
1578-
poll& set_question(const std::string& text);
1578+
poll& set_question(std::string_view text);
15791579

15801580
/**
15811581
* @brief Set the duration of the poll in hours
@@ -1611,7 +1611,7 @@ struct DPP_EXPORT poll {
16111611
* @param is_animated Whether the emoji is animated
16121612
* @return self for method chaining
16131613
*/
1614-
poll& add_answer(const std::string& text, snowflake emoji_id = 0, bool is_animated = false);
1614+
poll& add_answer(std::string_view text, snowflake emoji_id = 0, bool is_animated = false);
16151615

16161616
/**
16171617
* @brief Add an answer to this poll
@@ -1621,7 +1621,7 @@ struct DPP_EXPORT poll {
16211621
* @param emoji Optional emoji
16221622
* @return self for method chaining
16231623
*/
1624-
poll& add_answer(const std::string& text, const std::string& emoji);
1624+
poll& add_answer(std::string_view text, std::string_view emoji);
16251625

16261626
/**
16271627
* @brief Add an answer to this poll
@@ -1631,7 +1631,7 @@ struct DPP_EXPORT poll {
16311631
* @param e Optional emoji
16321632
* @return self for method chaining
16331633
*/
1634-
poll& add_answer(const std::string& text, const emoji& e);
1634+
poll& add_answer(std::string_view text, const emoji& e);
16351635

16361636
/**
16371637
* @brief Helper to get the question text
@@ -2286,7 +2286,7 @@ struct DPP_EXPORT message : public managed, json_interface<message> {
22862286
* @param content The content of the message. It will be truncated to the maximum length of 4000 UTF-8 characters.
22872287
* @param type The message type to create
22882288
*/
2289-
message(snowflake channel_id, const std::string &content, message_type type = mt_default);
2289+
message(snowflake channel_id, std::string_view content, message_type type = mt_default);
22902290

22912291
/**
22922292
* @brief Construct a new message object with content
@@ -2309,7 +2309,7 @@ struct DPP_EXPORT message : public managed, json_interface<message> {
23092309
* @param content The content of the message. It will be truncated to the maximum length of 4000 UTF-8 characters.
23102310
* @param type The message type to create
23112311
*/
2312-
message(const std::string &content, message_type type = mt_default);
2312+
message(std::string_view content, message_type type = mt_default);
23132313

23142314
/**
23152315
* @brief Destroy the message object
@@ -2516,34 +2516,34 @@ struct DPP_EXPORT message : public managed, json_interface<message> {
25162516
* @return message& reference to self
25172517
* @deprecated Use message::add_file instead
25182518
*/
2519-
message& set_filename(const std::string &fn);
2519+
message& set_filename(std::string_view fn);
25202520

25212521
/**
25222522
* @brief Set the file content of the last file in list
25232523
*
2524-
* @param fc raw file content contained in std::string
2524+
* @param fc raw file content
25252525
* @return message& reference to self
25262526
* @deprecated Use message::add_file instead
25272527
*/
2528-
message& set_file_content(const std::string &fc);
2528+
message& set_file_content(std::string_view fc);
25292529

25302530
/**
25312531
* @brief Add a file to the message
25322532
*
25332533
* @param filename filename
2534-
* @param filecontent raw file content contained in std::string
2534+
* @param filecontent raw file content
25352535
* @param filemimetype optional mime type of the file
25362536
* @return message& reference to self
25372537
*/
2538-
message& add_file(const std::string &filename, const std::string &filecontent, const std::string &filemimetype = "");
2538+
message& add_file(std::string_view filename, std::string_view filecontent, std::string_view filemimetype = "");
25392539

25402540
/**
25412541
* @brief Set the message content
25422542
*
25432543
* @param c message content. It will be truncated to the maximum length of 4000 UTF-8 characters.
25442544
* @return message& reference to self
25452545
*/
2546-
message& set_content(const std::string &c);
2546+
message& set_content(std::string_view c);
25472547

25482548
/**
25492549
* @brief Set the channel id

0 commit comments

Comments
 (0)