ustring.cpp 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516
  1. /*************************************************************************/
  2. /* ustring.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifdef _MSC_VER
  31. #define _CRT_SECURE_NO_WARNINGS // to disable build-time warning which suggested to use strcpy_s instead strcpy
  32. #endif
  33. #include "ustring.h"
  34. #include "core/color.h"
  35. #include "core/crypto/crypto_core.h"
  36. #include "core/math/math_funcs.h"
  37. #include "core/os/memory.h"
  38. #include "core/print_string.h"
  39. #include "core/translation.h"
  40. #include "core/ucaps.h"
  41. #include "core/variant.h"
  42. #include <wchar.h>
  43. #include <cstdint>
  44. #ifndef NO_USE_STDLIB
  45. #include <stdio.h>
  46. #include <stdlib.h>
  47. #endif
  48. #if defined(MINGW_ENABLED) || defined(_MSC_VER)
  49. #define snprintf _snprintf_s
  50. #endif
  51. #define MAX_DIGITS 6
  52. #define UPPERCASE(m_c) (((m_c) >= 'a' && (m_c) <= 'z') ? ((m_c) - ('a' - 'A')) : (m_c))
  53. #define LOWERCASE(m_c) (((m_c) >= 'A' && (m_c) <= 'Z') ? ((m_c) + ('a' - 'A')) : (m_c))
  54. #define IS_DIGIT(m_d) ((m_d) >= '0' && (m_d) <= '9')
  55. #define IS_HEX_DIGIT(m_d) (((m_d) >= '0' && (m_d) <= '9') || ((m_d) >= 'a' && (m_d) <= 'f') || ((m_d) >= 'A' && (m_d) <= 'F'))
  56. const char CharString::_null = 0;
  57. const CharType String::_null = 0;
  58. bool is_symbol(CharType c) {
  59. return c != '_' && ((c >= '!' && c <= '/') || (c >= ':' && c <= '@') || (c >= '[' && c <= '`') || (c >= '{' && c <= '~') || c == '\t' || c == ' ');
  60. }
  61. bool select_word(const String &p_s, int p_col, int &r_beg, int &r_end) {
  62. const String &s = p_s;
  63. int beg = CLAMP(p_col, 0, s.length());
  64. int end = beg;
  65. if (s[beg] > 32 || beg == s.length()) {
  66. bool symbol = beg < s.length() && is_symbol(s[beg]);
  67. while (beg > 0 && s[beg - 1] > 32 && (symbol == is_symbol(s[beg - 1]))) {
  68. beg--;
  69. }
  70. while (end < s.length() && s[end + 1] > 32 && (symbol == is_symbol(s[end + 1]))) {
  71. end++;
  72. }
  73. if (end < s.length()) {
  74. end += 1;
  75. }
  76. r_beg = beg;
  77. r_end = end;
  78. return true;
  79. } else {
  80. return false;
  81. }
  82. }
  83. /** STRING **/
  84. bool CharString::operator<(const CharString &p_right) const {
  85. if (length() == 0) {
  86. return p_right.length() != 0;
  87. }
  88. return is_str_less(get_data(), p_right.get_data());
  89. }
  90. CharString &CharString::operator+=(char p_char) {
  91. resize(size() ? size() + 1 : 2);
  92. set(length(), 0);
  93. set(length() - 1, p_char);
  94. return *this;
  95. }
  96. const char *CharString::get_data() const {
  97. if (size()) {
  98. return &operator[](0);
  99. } else {
  100. return "";
  101. }
  102. }
  103. CharString &CharString::operator=(const char *p_cstr) {
  104. copy_from(p_cstr);
  105. return *this;
  106. }
  107. void CharString::copy_from(const char *p_cstr) {
  108. if (!p_cstr) {
  109. resize(0);
  110. return;
  111. }
  112. size_t len = strlen(p_cstr);
  113. if (len == 0) {
  114. resize(0);
  115. return;
  116. }
  117. resize(len + 1); // include terminating null char
  118. strcpy(ptrw(), p_cstr);
  119. }
  120. Error String::parse_url(String &r_scheme, String &r_host, int &r_port, String &r_path) const {
  121. // Splits the URL into scheme, host, port, path. Strip credentials when present.
  122. String base = *this;
  123. r_scheme = "";
  124. r_host = "";
  125. r_port = 0;
  126. r_path = "";
  127. int pos = base.find("://");
  128. // Scheme
  129. if (pos != -1) {
  130. r_scheme = base.substr(0, pos + 3).to_lower();
  131. base = base.substr(pos + 3, base.length() - pos - 3);
  132. }
  133. pos = base.find("/");
  134. // Path
  135. if (pos != -1) {
  136. r_path = base.substr(pos, base.length() - pos);
  137. base = base.substr(0, pos);
  138. }
  139. // Host
  140. pos = base.find("@");
  141. if (pos != -1) {
  142. // Strip credentials
  143. base = base.substr(pos + 1, base.length() - pos - 1);
  144. }
  145. if (base.begins_with("[")) {
  146. // Literal IPv6
  147. pos = base.rfind("]");
  148. if (pos == -1) {
  149. return ERR_INVALID_PARAMETER;
  150. }
  151. r_host = base.substr(1, pos - 1);
  152. base = base.substr(pos + 1, base.length() - pos - 1);
  153. } else {
  154. // Anything else
  155. if (base.get_slice_count(":") > 2) {
  156. return ERR_INVALID_PARAMETER;
  157. }
  158. pos = base.rfind(":");
  159. if (pos == -1) {
  160. r_host = base;
  161. base = "";
  162. } else {
  163. r_host = base.substr(0, pos);
  164. base = base.substr(pos, base.length() - pos);
  165. }
  166. }
  167. if (r_host.empty()) {
  168. return ERR_INVALID_PARAMETER;
  169. }
  170. r_host = r_host.to_lower();
  171. // Port
  172. if (base.begins_with(":")) {
  173. base = base.substr(1, base.length() - 1);
  174. if (!base.is_valid_integer()) {
  175. return ERR_INVALID_PARAMETER;
  176. }
  177. r_port = base.to_int();
  178. if (r_port < 1 || r_port > 65535) {
  179. return ERR_INVALID_PARAMETER;
  180. }
  181. }
  182. return OK;
  183. }
  184. void String::copy_from(const char *p_cstr) {
  185. if (!p_cstr) {
  186. resize(0);
  187. return;
  188. }
  189. int len = 0;
  190. const char *ptr = p_cstr;
  191. while (*(ptr++) != 0) {
  192. len++;
  193. }
  194. if (len == 0) {
  195. resize(0);
  196. return;
  197. }
  198. resize(len + 1); // include 0
  199. CharType *dst = this->ptrw();
  200. for (int i = 0; i < len + 1; i++) {
  201. dst[i] = p_cstr[i];
  202. }
  203. }
  204. void String::copy_from(const CharType *p_cstr, const int p_clip_to) {
  205. if (!p_cstr) {
  206. resize(0);
  207. return;
  208. }
  209. int len = 0;
  210. const CharType *ptr = p_cstr;
  211. while ((p_clip_to < 0 || len < p_clip_to) && *(ptr++) != 0) {
  212. len++;
  213. }
  214. if (len == 0) {
  215. resize(0);
  216. return;
  217. }
  218. copy_from_unchecked(p_cstr, len);
  219. }
  220. // assumes the following have already been validated:
  221. // p_char != NULL
  222. // p_length > 0
  223. // p_length <= p_char strlen
  224. void String::copy_from_unchecked(const CharType *p_char, const int p_length) {
  225. resize(p_length + 1);
  226. set(p_length, 0);
  227. CharType *dst = ptrw();
  228. for (int i = 0; i < p_length; i++) {
  229. dst[i] = p_char[i];
  230. }
  231. }
  232. void String::copy_from(const CharType &p_char) {
  233. resize(2);
  234. set(0, p_char);
  235. set(1, 0);
  236. }
  237. bool String::operator==(const String &p_str) const {
  238. if (length() != p_str.length()) {
  239. return false;
  240. }
  241. if (empty()) {
  242. return true;
  243. }
  244. int l = length();
  245. const CharType *src = c_str();
  246. const CharType *dst = p_str.c_str();
  247. /* Compare char by char */
  248. for (int i = 0; i < l; i++) {
  249. if (src[i] != dst[i]) {
  250. return false;
  251. }
  252. }
  253. return true;
  254. }
  255. bool String::operator!=(const String &p_str) const {
  256. return !(*this == p_str);
  257. }
  258. String String::operator+(const String &p_str) const {
  259. String res = *this;
  260. res += p_str;
  261. return res;
  262. }
  263. String &String::operator+=(const String &p_str) {
  264. if (empty()) {
  265. *this = p_str;
  266. return *this;
  267. }
  268. if (p_str.empty()) {
  269. return *this;
  270. }
  271. int from = length();
  272. resize(length() + p_str.size());
  273. const CharType *src = p_str.c_str();
  274. CharType *dst = ptrw();
  275. set(length(), 0);
  276. for (int i = 0; i < p_str.length(); i++) {
  277. dst[from + i] = src[i];
  278. }
  279. return *this;
  280. }
  281. String &String::operator+=(const CharType *p_str) {
  282. *this += String(p_str);
  283. return *this;
  284. }
  285. String &String::operator+=(CharType p_char) {
  286. resize(size() ? size() + 1 : 2);
  287. set(length(), 0);
  288. set(length() - 1, p_char);
  289. return *this;
  290. }
  291. String &String::operator+=(const char *p_str) {
  292. if (!p_str || p_str[0] == 0) {
  293. return *this;
  294. }
  295. int src_len = 0;
  296. const char *ptr = p_str;
  297. while (*(ptr++) != 0) {
  298. src_len++;
  299. }
  300. int from = length();
  301. resize(from + src_len + 1);
  302. CharType *dst = ptrw();
  303. set(length(), 0);
  304. for (int i = 0; i < src_len; i++) {
  305. dst[from + i] = p_str[i];
  306. }
  307. return *this;
  308. }
  309. void String::operator=(const char *p_str) {
  310. copy_from(p_str);
  311. }
  312. void String::operator=(const CharType *p_str) {
  313. copy_from(p_str);
  314. }
  315. bool String::operator==(const StrRange &p_str_range) const {
  316. int len = p_str_range.len;
  317. if (length() != len) {
  318. return false;
  319. }
  320. if (empty()) {
  321. return true;
  322. }
  323. const CharType *c_str = p_str_range.c_str;
  324. const CharType *dst = &operator[](0);
  325. /* Compare char by char */
  326. for (int i = 0; i < len; i++) {
  327. if (c_str[i] != dst[i]) {
  328. return false;
  329. }
  330. }
  331. return true;
  332. }
  333. bool String::operator==(const char *p_str) const {
  334. int len = 0;
  335. const char *aux = p_str;
  336. while (*(aux++) != 0) {
  337. len++;
  338. }
  339. if (length() != len) {
  340. return false;
  341. }
  342. if (empty()) {
  343. return true;
  344. }
  345. int l = length();
  346. const CharType *dst = c_str();
  347. /* Compare char by char */
  348. for (int i = 0; i < l; i++) {
  349. if (p_str[i] != dst[i]) {
  350. return false;
  351. }
  352. }
  353. return true;
  354. }
  355. bool String::operator==(const CharType *p_str) const {
  356. int len = 0;
  357. const CharType *aux = p_str;
  358. while (*(aux++) != 0) {
  359. len++;
  360. }
  361. if (length() != len) {
  362. return false;
  363. }
  364. if (empty()) {
  365. return true;
  366. }
  367. int l = length();
  368. const CharType *dst = c_str();
  369. /* Compare char by char */
  370. for (int i = 0; i < l; i++) {
  371. if (p_str[i] != dst[i]) {
  372. return false;
  373. }
  374. }
  375. return true;
  376. }
  377. bool String::operator!=(const char *p_str) const {
  378. return (!(*this == p_str));
  379. }
  380. bool String::operator!=(const CharType *p_str) const {
  381. return (!(*this == p_str));
  382. }
  383. bool String::operator<(const CharType *p_str) const {
  384. if (empty() && p_str[0] == 0) {
  385. return false;
  386. }
  387. if (empty()) {
  388. return true;
  389. }
  390. return is_str_less(c_str(), p_str);
  391. }
  392. bool String::operator<=(const String &p_str) const {
  393. return (*this < p_str) || (*this == p_str);
  394. }
  395. bool String::operator<(const char *p_str) const {
  396. if (empty() && p_str[0] == 0) {
  397. return false;
  398. }
  399. if (empty()) {
  400. return true;
  401. }
  402. return is_str_less(c_str(), p_str);
  403. }
  404. bool String::operator<(const String &p_str) const {
  405. return operator<(p_str.c_str());
  406. }
  407. signed char String::nocasecmp_to(const String &p_str) const {
  408. if (empty() && p_str.empty()) {
  409. return 0;
  410. }
  411. if (empty()) {
  412. return -1;
  413. }
  414. if (p_str.empty()) {
  415. return 1;
  416. }
  417. const CharType *that_str = p_str.c_str();
  418. const CharType *this_str = c_str();
  419. while (true) {
  420. if (*that_str == 0 && *this_str == 0) {
  421. return 0; //we're equal
  422. } else if (*this_str == 0) {
  423. return -1; //if this is empty, and the other one is not, then we're less.. I think?
  424. } else if (*that_str == 0) {
  425. return 1; //otherwise the other one is smaller..
  426. } else if (_find_upper(*this_str) < _find_upper(*that_str)) { //more than
  427. return -1;
  428. } else if (_find_upper(*this_str) > _find_upper(*that_str)) { //less than
  429. return 1;
  430. }
  431. this_str++;
  432. that_str++;
  433. }
  434. }
  435. signed char String::casecmp_to(const String &p_str) const {
  436. if (empty() && p_str.empty()) {
  437. return 0;
  438. }
  439. if (empty()) {
  440. return -1;
  441. }
  442. if (p_str.empty()) {
  443. return 1;
  444. }
  445. const CharType *that_str = p_str.c_str();
  446. const CharType *this_str = c_str();
  447. while (true) {
  448. if (*that_str == 0 && *this_str == 0) {
  449. return 0; //we're equal
  450. } else if (*this_str == 0) {
  451. return -1; //if this is empty, and the other one is not, then we're less.. I think?
  452. } else if (*that_str == 0) {
  453. return 1; //otherwise the other one is smaller..
  454. } else if (*this_str < *that_str) { //more than
  455. return -1;
  456. } else if (*this_str > *that_str) { //less than
  457. return 1;
  458. }
  459. this_str++;
  460. that_str++;
  461. }
  462. }
  463. signed char String::naturalnocasecmp_to(const String &p_str) const {
  464. const CharType *this_str = c_str();
  465. const CharType *that_str = p_str.c_str();
  466. if (this_str && that_str) {
  467. while (*this_str == '.' || *that_str == '.') {
  468. if (*this_str++ != '.') {
  469. return 1;
  470. }
  471. if (*that_str++ != '.') {
  472. return -1;
  473. }
  474. if (!*that_str) {
  475. return 1;
  476. }
  477. if (!*this_str) {
  478. return -1;
  479. }
  480. }
  481. while (*this_str) {
  482. if (!*that_str) {
  483. return 1;
  484. } else if (IS_DIGIT(*this_str)) {
  485. if (!IS_DIGIT(*that_str)) {
  486. return -1;
  487. }
  488. // Keep ptrs to start of numerical sequences
  489. const CharType *this_substr = this_str;
  490. const CharType *that_substr = that_str;
  491. // Compare lengths of both numerical sequences, ignoring leading zeros
  492. while (IS_DIGIT(*this_str)) {
  493. this_str++;
  494. }
  495. while (IS_DIGIT(*that_str)) {
  496. that_str++;
  497. }
  498. while (*this_substr == '0') {
  499. this_substr++;
  500. }
  501. while (*that_substr == '0') {
  502. that_substr++;
  503. }
  504. int this_len = this_str - this_substr;
  505. int that_len = that_str - that_substr;
  506. if (this_len < that_len) {
  507. return -1;
  508. } else if (this_len > that_len) {
  509. return 1;
  510. }
  511. // If lengths equal, compare lexicographically
  512. while (this_substr != this_str && that_substr != that_str) {
  513. if (*this_substr < *that_substr) {
  514. return -1;
  515. } else if (*this_substr > *that_substr) {
  516. return 1;
  517. }
  518. this_substr++;
  519. that_substr++;
  520. }
  521. } else if (IS_DIGIT(*that_str)) {
  522. return 1;
  523. } else {
  524. if (_find_upper(*this_str) < _find_upper(*that_str)) { //more than
  525. return -1;
  526. } else if (_find_upper(*this_str) > _find_upper(*that_str)) { //less than
  527. return 1;
  528. }
  529. this_str++;
  530. that_str++;
  531. }
  532. }
  533. if (*that_str) {
  534. return -1;
  535. }
  536. }
  537. return 0;
  538. }
  539. void String::erase(int p_pos, int p_chars) {
  540. *this = left(p_pos) + substr(p_pos + p_chars, length() - ((p_pos + p_chars)));
  541. }
  542. String String::capitalize() const {
  543. String aux = this->camelcase_to_underscore(true).replace("_", " ").strip_edges();
  544. String cap;
  545. for (int i = 0; i < aux.get_slice_count(" "); i++) {
  546. String slice = aux.get_slicec(' ', i);
  547. if (slice.length() > 0) {
  548. slice[0] = _find_upper(slice[0]);
  549. if (i > 0) {
  550. cap += " ";
  551. }
  552. cap += slice;
  553. }
  554. }
  555. return cap;
  556. }
  557. String String::camelcase_to_underscore(bool lowercase) const {
  558. const CharType *cstr = c_str();
  559. String new_string;
  560. const char A = 'A', Z = 'Z';
  561. const char a = 'a', z = 'z';
  562. int start_index = 0;
  563. for (int i = 1; i < this->size(); i++) {
  564. bool is_upper = cstr[i] >= A && cstr[i] <= Z;
  565. bool is_number = cstr[i] >= '0' && cstr[i] <= '9';
  566. bool are_next_2_lower = false;
  567. bool is_next_lower = false;
  568. bool is_next_number = false;
  569. bool was_precedent_upper = cstr[i - 1] >= A && cstr[i - 1] <= Z;
  570. bool was_precedent_number = cstr[i - 1] >= '0' && cstr[i - 1] <= '9';
  571. if (i + 2 < this->size()) {
  572. are_next_2_lower = cstr[i + 1] >= a && cstr[i + 1] <= z && cstr[i + 2] >= a && cstr[i + 2] <= z;
  573. }
  574. if (i + 1 < this->size()) {
  575. is_next_lower = cstr[i + 1] >= a && cstr[i + 1] <= z;
  576. is_next_number = cstr[i + 1] >= '0' && cstr[i + 1] <= '9';
  577. }
  578. const bool cond_a = is_upper && !was_precedent_upper && !was_precedent_number;
  579. const bool cond_b = was_precedent_upper && is_upper && are_next_2_lower;
  580. const bool cond_c = is_number && !was_precedent_number;
  581. const bool can_break_number_letter = is_number && !was_precedent_number && is_next_lower;
  582. const bool can_break_letter_number = !is_number && was_precedent_number && (is_next_lower || is_next_number);
  583. bool should_split = cond_a || cond_b || cond_c || can_break_number_letter || can_break_letter_number;
  584. if (should_split) {
  585. new_string += this->substr(start_index, i - start_index) + "_";
  586. start_index = i;
  587. }
  588. }
  589. new_string += this->substr(start_index, this->size() - start_index);
  590. return lowercase ? new_string.to_lower() : new_string;
  591. }
  592. int String::get_slice_count(String p_splitter) const {
  593. if (empty()) {
  594. return 0;
  595. }
  596. if (p_splitter.empty()) {
  597. return 0;
  598. }
  599. int pos = 0;
  600. int slices = 1;
  601. while ((pos = find(p_splitter, pos)) >= 0) {
  602. slices++;
  603. pos += p_splitter.length();
  604. }
  605. return slices;
  606. }
  607. String String::get_slice(String p_splitter, int p_slice) const {
  608. if (empty() || p_splitter.empty()) {
  609. return "";
  610. }
  611. int pos = 0;
  612. int prev_pos = 0;
  613. //int slices=1;
  614. if (p_slice < 0) {
  615. return "";
  616. }
  617. if (find(p_splitter) == -1) {
  618. return *this;
  619. }
  620. int i = 0;
  621. while (true) {
  622. pos = find(p_splitter, pos);
  623. if (pos == -1) {
  624. pos = length(); //reached end
  625. }
  626. int from = prev_pos;
  627. //int to=pos;
  628. if (p_slice == i) {
  629. return substr(from, pos - from);
  630. }
  631. if (pos == length()) { //reached end and no find
  632. break;
  633. }
  634. pos += p_splitter.length();
  635. prev_pos = pos;
  636. i++;
  637. }
  638. return ""; //no find!
  639. }
  640. String String::get_slicec(CharType p_splitter, int p_slice) const {
  641. if (empty()) {
  642. return String();
  643. }
  644. if (p_slice < 0) {
  645. return String();
  646. }
  647. const CharType *c = this->ptr();
  648. int i = 0;
  649. int prev = 0;
  650. int count = 0;
  651. while (true) {
  652. if (c[i] == 0 || c[i] == p_splitter) {
  653. if (p_slice == count) {
  654. return substr(prev, i - prev);
  655. } else if (c[i] == 0) {
  656. return String();
  657. } else {
  658. count++;
  659. prev = i + 1;
  660. }
  661. }
  662. i++;
  663. }
  664. }
  665. Vector<String> String::split_spaces() const {
  666. Vector<String> ret;
  667. int from = 0;
  668. int i = 0;
  669. int len = length();
  670. if (len == 0) {
  671. return ret;
  672. }
  673. bool inside = false;
  674. while (true) {
  675. bool empty = operator[](i) < 33;
  676. if (i == 0) {
  677. inside = !empty;
  678. }
  679. if (!empty && !inside) {
  680. inside = true;
  681. from = i;
  682. }
  683. if (empty && inside) {
  684. ret.push_back(substr(from, i - from));
  685. inside = false;
  686. }
  687. if (i == len) {
  688. break;
  689. }
  690. i++;
  691. }
  692. return ret;
  693. }
  694. Vector<String> String::split(const String &p_splitter, bool p_allow_empty, int p_maxsplit) const {
  695. Vector<String> ret;
  696. int from = 0;
  697. int len = length();
  698. while (true) {
  699. int end = find(p_splitter, from);
  700. if (end < 0) {
  701. end = len;
  702. }
  703. if (p_allow_empty || (end > from)) {
  704. if (p_maxsplit <= 0) {
  705. ret.push_back(substr(from, end - from));
  706. } else {
  707. // Put rest of the string and leave cycle.
  708. if (p_maxsplit == ret.size()) {
  709. ret.push_back(substr(from, len));
  710. break;
  711. }
  712. // Otherwise, push items until positive limit is reached.
  713. ret.push_back(substr(from, end - from));
  714. }
  715. }
  716. if (end == len) {
  717. break;
  718. }
  719. from = end + p_splitter.length();
  720. }
  721. return ret;
  722. }
  723. Vector<String> String::rsplit(const String &p_splitter, bool p_allow_empty, int p_maxsplit) const {
  724. Vector<String> ret;
  725. const int len = length();
  726. int remaining_len = len;
  727. while (true) {
  728. if (remaining_len < p_splitter.length() || (p_maxsplit > 0 && p_maxsplit == ret.size())) {
  729. // no room for another splitter or hit max splits, push what's left and we're done
  730. if (p_allow_empty || remaining_len > 0) {
  731. ret.push_back(substr(0, remaining_len));
  732. }
  733. break;
  734. }
  735. int left_edge = rfind(p_splitter, remaining_len - p_splitter.length());
  736. if (left_edge < 0) {
  737. // no more splitters, we're done
  738. ret.push_back(substr(0, remaining_len));
  739. break;
  740. }
  741. int substr_start = left_edge + p_splitter.length();
  742. if (p_allow_empty || substr_start < remaining_len) {
  743. ret.push_back(substr(substr_start, remaining_len - substr_start));
  744. }
  745. remaining_len = left_edge;
  746. }
  747. ret.invert();
  748. return ret;
  749. }
  750. Vector<float> String::split_floats(const String &p_splitter, bool p_allow_empty) const {
  751. Vector<float> ret;
  752. int from = 0;
  753. int len = length();
  754. while (true) {
  755. int end = find(p_splitter, from);
  756. if (end < 0) {
  757. end = len;
  758. }
  759. if (p_allow_empty || (end > from)) {
  760. ret.push_back(String::to_double(&c_str()[from]));
  761. }
  762. if (end == len) {
  763. break;
  764. }
  765. from = end + p_splitter.length();
  766. }
  767. return ret;
  768. }
  769. Vector<float> String::split_floats_mk(const Vector<String> &p_splitters, bool p_allow_empty) const {
  770. Vector<float> ret;
  771. int from = 0;
  772. int len = length();
  773. while (true) {
  774. int idx;
  775. int end = findmk(p_splitters, from, &idx);
  776. int spl_len = 1;
  777. if (end < 0) {
  778. end = len;
  779. } else {
  780. spl_len = p_splitters[idx].length();
  781. }
  782. if (p_allow_empty || (end > from)) {
  783. ret.push_back(String::to_double(&c_str()[from]));
  784. }
  785. if (end == len) {
  786. break;
  787. }
  788. from = end + spl_len;
  789. }
  790. return ret;
  791. }
  792. Vector<int> String::split_ints(const String &p_splitter, bool p_allow_empty) const {
  793. Vector<int> ret;
  794. int from = 0;
  795. int len = length();
  796. while (true) {
  797. int end = find(p_splitter, from);
  798. if (end < 0) {
  799. end = len;
  800. }
  801. if (p_allow_empty || (end > from)) {
  802. ret.push_back(String::to_int(&c_str()[from], end - from));
  803. }
  804. if (end == len) {
  805. break;
  806. }
  807. from = end + p_splitter.length();
  808. }
  809. return ret;
  810. }
  811. Vector<int> String::split_ints_mk(const Vector<String> &p_splitters, bool p_allow_empty) const {
  812. Vector<int> ret;
  813. int from = 0;
  814. int len = length();
  815. while (true) {
  816. int idx;
  817. int end = findmk(p_splitters, from, &idx);
  818. int spl_len = 1;
  819. if (end < 0) {
  820. end = len;
  821. } else {
  822. spl_len = p_splitters[idx].length();
  823. }
  824. if (p_allow_empty || (end > from)) {
  825. ret.push_back(String::to_int(&c_str()[from], end - from));
  826. }
  827. if (end == len) {
  828. break;
  829. }
  830. from = end + spl_len;
  831. }
  832. return ret;
  833. }
  834. String String::join(Vector<String> parts) {
  835. String ret;
  836. for (int i = 0; i < parts.size(); ++i) {
  837. if (i > 0) {
  838. ret += *this;
  839. }
  840. ret += parts[i];
  841. }
  842. return ret;
  843. }
  844. CharType String::char_uppercase(CharType p_char) {
  845. return _find_upper(p_char);
  846. }
  847. CharType String::char_lowercase(CharType p_char) {
  848. return _find_lower(p_char);
  849. }
  850. String String::to_upper() const {
  851. String upper = *this;
  852. for (int i = 0; i < upper.size(); i++) {
  853. const CharType s = upper[i];
  854. const CharType t = _find_upper(s);
  855. if (s != t) { // avoid copy on write
  856. upper[i] = t;
  857. }
  858. }
  859. return upper;
  860. }
  861. String String::to_lower() const {
  862. String lower = *this;
  863. for (int i = 0; i < lower.size(); i++) {
  864. const CharType s = lower[i];
  865. const CharType t = _find_lower(s);
  866. if (s != t) { // avoid copy on write
  867. lower[i] = t;
  868. }
  869. }
  870. return lower;
  871. }
  872. const CharType *String::c_str() const {
  873. static const CharType zero = 0;
  874. return size() ? &operator[](0) : &zero;
  875. }
  876. String String::md5(const uint8_t *p_md5) {
  877. return String::hex_encode_buffer(p_md5, 16);
  878. }
  879. String String::hex_encode_buffer(const uint8_t *p_buffer, int p_len) {
  880. static const char hex[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
  881. String ret;
  882. char v[2] = { 0, 0 };
  883. for (int i = 0; i < p_len; i++) {
  884. v[0] = hex[p_buffer[i] >> 4];
  885. ret += v;
  886. v[0] = hex[p_buffer[i] & 0xF];
  887. ret += v;
  888. }
  889. return ret;
  890. }
  891. String String::chr(CharType p_char) {
  892. CharType c[2] = { p_char, 0 };
  893. return String(c);
  894. }
  895. String String::num(double p_num, int p_decimals) {
  896. if (Math::is_nan(p_num)) {
  897. return "nan";
  898. }
  899. #ifndef NO_USE_STDLIB
  900. if (p_decimals > 16) {
  901. p_decimals = 16;
  902. }
  903. char fmt[7];
  904. fmt[0] = '%';
  905. fmt[1] = '.';
  906. if (p_decimals < 0) {
  907. fmt[1] = 'l';
  908. fmt[2] = 'f';
  909. fmt[3] = 0;
  910. } else if (p_decimals < 10) {
  911. fmt[2] = '0' + p_decimals;
  912. fmt[3] = 'l';
  913. fmt[4] = 'f';
  914. fmt[5] = 0;
  915. } else {
  916. fmt[2] = '0' + (p_decimals / 10);
  917. fmt[3] = '0' + (p_decimals % 10);
  918. fmt[4] = 'l';
  919. fmt[5] = 'f';
  920. fmt[6] = 0;
  921. }
  922. char buf[256];
  923. #if defined(__GNUC__) || defined(_MSC_VER)
  924. snprintf(buf, 256, fmt, p_num);
  925. #else
  926. sprintf(buf, fmt, p_num);
  927. #endif
  928. buf[255] = 0;
  929. //destroy trailing zeroes
  930. {
  931. bool period = false;
  932. int z = 0;
  933. while (buf[z]) {
  934. if (buf[z] == '.') {
  935. period = true;
  936. }
  937. z++;
  938. }
  939. if (period) {
  940. z--;
  941. while (z > 0) {
  942. if (buf[z] == '0') {
  943. buf[z] = 0;
  944. } else if (buf[z] == '.') {
  945. buf[z] = 0;
  946. break;
  947. } else {
  948. break;
  949. }
  950. z--;
  951. }
  952. }
  953. }
  954. return buf;
  955. #else
  956. String s;
  957. String sd;
  958. /* integer part */
  959. bool neg = p_num < 0;
  960. p_num = ABS(p_num);
  961. int intn = (int)p_num;
  962. /* decimal part */
  963. if (p_decimals > 0 || (p_decimals == -1 && (int)p_num != p_num)) {
  964. double dec = p_num - (float)((int)p_num);
  965. int digit = 0;
  966. if (p_decimals > MAX_DIGITS)
  967. p_decimals = MAX_DIGITS;
  968. int dec_int = 0;
  969. int dec_max = 0;
  970. while (true) {
  971. dec *= 10.0;
  972. dec_int = dec_int * 10 + (int)dec % 10;
  973. dec_max = dec_max * 10 + 9;
  974. digit++;
  975. if (p_decimals == -1) {
  976. if (digit == MAX_DIGITS) //no point in going to infinite
  977. break;
  978. if ((dec - (float)((int)dec)) < 1e-6)
  979. break;
  980. }
  981. if (digit == p_decimals)
  982. break;
  983. }
  984. dec *= 10;
  985. int last = (int)dec % 10;
  986. if (last > 5) {
  987. if (dec_int == dec_max) {
  988. dec_int = 0;
  989. intn++;
  990. } else {
  991. dec_int++;
  992. }
  993. }
  994. String decimal;
  995. for (int i = 0; i < digit; i++) {
  996. char num[2] = { 0, 0 };
  997. num[0] = '0' + dec_int % 10;
  998. decimal = num + decimal;
  999. dec_int /= 10;
  1000. }
  1001. sd = '.' + decimal;
  1002. }
  1003. if (intn == 0)
  1004. s = "0";
  1005. else {
  1006. while (intn) {
  1007. CharType num = '0' + (intn % 10);
  1008. intn /= 10;
  1009. s = num + s;
  1010. }
  1011. }
  1012. s = s + sd;
  1013. if (neg)
  1014. s = "-" + s;
  1015. return s;
  1016. #endif
  1017. }
  1018. String String::num_int64(int64_t p_num, int base, bool capitalize_hex) {
  1019. bool sign = p_num < 0;
  1020. int64_t n = p_num;
  1021. int chars = 0;
  1022. do {
  1023. n /= base;
  1024. chars++;
  1025. } while (n);
  1026. if (sign) {
  1027. chars++;
  1028. }
  1029. String s;
  1030. s.resize(chars + 1);
  1031. CharType *c = s.ptrw();
  1032. c[chars] = 0;
  1033. n = p_num;
  1034. do {
  1035. int mod = ABS(n % base);
  1036. if (mod >= 10) {
  1037. char a = (capitalize_hex ? 'A' : 'a');
  1038. c[--chars] = a + (mod - 10);
  1039. } else {
  1040. c[--chars] = '0' + mod;
  1041. }
  1042. n /= base;
  1043. } while (n);
  1044. if (sign) {
  1045. c[0] = '-';
  1046. }
  1047. return s;
  1048. }
  1049. String String::num_uint64(uint64_t p_num, int base, bool capitalize_hex) {
  1050. uint64_t n = p_num;
  1051. int chars = 0;
  1052. do {
  1053. n /= base;
  1054. chars++;
  1055. } while (n);
  1056. String s;
  1057. s.resize(chars + 1);
  1058. CharType *c = s.ptrw();
  1059. c[chars] = 0;
  1060. n = p_num;
  1061. do {
  1062. int mod = n % base;
  1063. if (mod >= 10) {
  1064. char a = (capitalize_hex ? 'A' : 'a');
  1065. c[--chars] = a + (mod - 10);
  1066. } else {
  1067. c[--chars] = '0' + mod;
  1068. }
  1069. n /= base;
  1070. } while (n);
  1071. return s;
  1072. }
  1073. String String::num_real(double p_num) {
  1074. String s;
  1075. String sd;
  1076. /* integer part */
  1077. bool neg = p_num < 0;
  1078. p_num = ABS(p_num);
  1079. int intn = (int)p_num;
  1080. /* decimal part */
  1081. if ((int)p_num != p_num) {
  1082. double dec = p_num - (float)((int)p_num);
  1083. int digit = 0;
  1084. int decimals = MAX_DIGITS;
  1085. int dec_int = 0;
  1086. int dec_max = 0;
  1087. while (true) {
  1088. dec *= 10.0;
  1089. dec_int = dec_int * 10 + (int)dec % 10;
  1090. dec_max = dec_max * 10 + 9;
  1091. digit++;
  1092. if ((dec - (float)((int)dec)) < 1e-6) {
  1093. break;
  1094. }
  1095. if (digit == decimals) {
  1096. break;
  1097. }
  1098. }
  1099. dec *= 10;
  1100. int last = (int)dec % 10;
  1101. if (last > 5) {
  1102. if (dec_int == dec_max) {
  1103. dec_int = 0;
  1104. intn++;
  1105. } else {
  1106. dec_int++;
  1107. }
  1108. }
  1109. String decimal;
  1110. for (int i = 0; i < digit; i++) {
  1111. char num[2] = { 0, 0 };
  1112. num[0] = '0' + dec_int % 10;
  1113. decimal = num + decimal;
  1114. dec_int /= 10;
  1115. }
  1116. sd = '.' + decimal;
  1117. } else {
  1118. sd = ".0";
  1119. }
  1120. if (intn == 0) {
  1121. s = "0";
  1122. } else {
  1123. while (intn) {
  1124. CharType num = '0' + (intn % 10);
  1125. intn /= 10;
  1126. s = num + s;
  1127. }
  1128. }
  1129. s = s + sd;
  1130. if (neg) {
  1131. s = "-" + s;
  1132. }
  1133. return s;
  1134. }
  1135. String String::num_scientific(double p_num) {
  1136. if (Math::is_nan(p_num)) {
  1137. return "nan";
  1138. }
  1139. #ifndef NO_USE_STDLIB
  1140. char buf[256];
  1141. #if defined(__GNUC__) || defined(_MSC_VER)
  1142. #if defined(__MINGW32__) && defined(_TWO_DIGIT_EXPONENT) && !defined(_UCRT)
  1143. // MinGW requires _set_output_format() to conform to C99 output for printf
  1144. unsigned int old_exponent_format = _set_output_format(_TWO_DIGIT_EXPONENT);
  1145. #endif
  1146. snprintf(buf, 256, "%lg", p_num);
  1147. #if defined(__MINGW32__) && defined(_TWO_DIGIT_EXPONENT) && !defined(_UCRT)
  1148. _set_output_format(old_exponent_format);
  1149. #endif
  1150. #else
  1151. sprintf(buf, "%.16lg", p_num);
  1152. #endif
  1153. buf[255] = 0;
  1154. return buf;
  1155. #else
  1156. return String::num(p_num);
  1157. #endif
  1158. }
  1159. CharString String::ascii(bool p_allow_extended) const {
  1160. if (!length()) {
  1161. return CharString();
  1162. }
  1163. CharString cs;
  1164. cs.resize(size());
  1165. for (int i = 0; i < size(); i++) {
  1166. cs[i] = operator[](i);
  1167. }
  1168. return cs;
  1169. }
  1170. String String::utf8(const char *p_utf8, int p_len) {
  1171. String ret;
  1172. ret.parse_utf8(p_utf8, p_len);
  1173. return ret;
  1174. };
  1175. bool String::parse_utf8(const char *p_utf8, int p_len) {
  1176. #define _UNICERROR(m_err) print_line("Unicode error: " + String(m_err));
  1177. if (!p_utf8) {
  1178. return true;
  1179. }
  1180. String aux;
  1181. int cstr_size = 0;
  1182. int str_size = 0;
  1183. /* HANDLE BOM (Byte Order Mark) */
  1184. if (p_len < 0 || p_len >= 3) {
  1185. bool has_bom = uint8_t(p_utf8[0]) == 0xEF && uint8_t(p_utf8[1]) == 0xBB && uint8_t(p_utf8[2]) == 0xBF;
  1186. if (has_bom) {
  1187. //just skip it
  1188. if (p_len >= 0) {
  1189. p_len -= 3;
  1190. }
  1191. p_utf8 += 3;
  1192. }
  1193. }
  1194. {
  1195. const char *ptrtmp = p_utf8;
  1196. const char *ptrtmp_limit = &p_utf8[p_len];
  1197. int skip = 0;
  1198. while (ptrtmp != ptrtmp_limit && *ptrtmp) {
  1199. if (skip == 0) {
  1200. uint8_t c = *ptrtmp >= 0 ? *ptrtmp : uint8_t(256 + *ptrtmp);
  1201. /* Determine the number of characters in sequence */
  1202. if ((c & 0x80) == 0) {
  1203. skip = 0;
  1204. } else if ((c & 0xE0) == 0xC0) {
  1205. skip = 1;
  1206. } else if ((c & 0xF0) == 0xE0) {
  1207. skip = 2;
  1208. } else if ((c & 0xF8) == 0xF0) {
  1209. skip = 3;
  1210. } else if ((c & 0xFC) == 0xF8) {
  1211. skip = 4;
  1212. } else if ((c & 0xFE) == 0xFC) {
  1213. skip = 5;
  1214. } else {
  1215. _UNICERROR("invalid skip");
  1216. return true; //invalid utf8
  1217. }
  1218. if (skip == 1 && (c & 0x1E) == 0) {
  1219. //printf("overlong rejected\n");
  1220. _UNICERROR("overlong rejected");
  1221. return true; //reject overlong
  1222. }
  1223. str_size++;
  1224. } else {
  1225. --skip;
  1226. }
  1227. cstr_size++;
  1228. ptrtmp++;
  1229. }
  1230. if (skip) {
  1231. _UNICERROR("no space left");
  1232. return true; //not enough spac
  1233. }
  1234. }
  1235. if (str_size == 0) {
  1236. clear();
  1237. return false;
  1238. }
  1239. resize(str_size + 1);
  1240. CharType *dst = ptrw();
  1241. dst[str_size] = 0;
  1242. while (cstr_size) {
  1243. int len = 0;
  1244. /* Determine the number of characters in sequence */
  1245. if ((*p_utf8 & 0x80) == 0) {
  1246. len = 1;
  1247. } else if ((*p_utf8 & 0xE0) == 0xC0) {
  1248. len = 2;
  1249. } else if ((*p_utf8 & 0xF0) == 0xE0) {
  1250. len = 3;
  1251. } else if ((*p_utf8 & 0xF8) == 0xF0) {
  1252. len = 4;
  1253. } else if ((*p_utf8 & 0xFC) == 0xF8) {
  1254. len = 5;
  1255. } else if ((*p_utf8 & 0xFE) == 0xFC) {
  1256. len = 6;
  1257. } else {
  1258. _UNICERROR("invalid len");
  1259. return true; //invalid UTF8
  1260. }
  1261. if (len > cstr_size) {
  1262. _UNICERROR("no space left");
  1263. return true; //not enough space
  1264. }
  1265. if (len == 2 && (*p_utf8 & 0x1E) == 0) {
  1266. //printf("overlong rejected\n");
  1267. _UNICERROR("no space left");
  1268. return true; //reject overlong
  1269. }
  1270. /* Convert the first character */
  1271. uint32_t unichar = 0;
  1272. if (len == 1) {
  1273. unichar = *p_utf8;
  1274. } else {
  1275. unichar = (0xFF >> (len + 1)) & *p_utf8;
  1276. for (int i = 1; i < len; i++) {
  1277. if ((p_utf8[i] & 0xC0) != 0x80) {
  1278. _UNICERROR("invalid utf8");
  1279. return true; //invalid utf8
  1280. }
  1281. if (unichar == 0 && i == 2 && ((p_utf8[i] & 0x7F) >> (7 - len)) == 0) {
  1282. _UNICERROR("invalid utf8 overlong");
  1283. return true; //no overlong
  1284. }
  1285. unichar = (unichar << 6) | (p_utf8[i] & 0x3F);
  1286. }
  1287. }
  1288. //printf("char %i, len %i\n",unichar,len);
  1289. if (sizeof(wchar_t) == 2 && unichar > 0xFFFF) {
  1290. unichar = ' '; //too long for windows
  1291. }
  1292. *(dst++) = unichar;
  1293. cstr_size -= len;
  1294. p_utf8 += len;
  1295. }
  1296. return false;
  1297. }
  1298. CharString String::utf8() const {
  1299. int l = length();
  1300. if (!l) {
  1301. return CharString();
  1302. }
  1303. const CharType *d = &operator[](0);
  1304. int fl = 0;
  1305. for (int i = 0; i < l; i++) {
  1306. uint32_t c = d[i];
  1307. if (c <= 0x7f) { // 7 bits.
  1308. fl += 1;
  1309. } else if (c <= 0x7ff) { // 11 bits
  1310. fl += 2;
  1311. } else if (c <= 0xffff) { // 16 bits
  1312. fl += 3;
  1313. } else if (c <= 0x001fffff) { // 21 bits
  1314. fl += 4;
  1315. } else if (c <= 0x03ffffff) { // 26 bits
  1316. fl += 5;
  1317. } else if (c <= 0x7fffffff) { // 31 bits
  1318. fl += 6;
  1319. }
  1320. }
  1321. CharString utf8s;
  1322. if (fl == 0) {
  1323. return utf8s;
  1324. }
  1325. utf8s.resize(fl + 1);
  1326. uint8_t *cdst = (uint8_t *)utf8s.get_data();
  1327. #define APPEND_CHAR(m_c) *(cdst++) = m_c
  1328. for (int i = 0; i < l; i++) {
  1329. uint32_t c = d[i];
  1330. if (c <= 0x7f) { // 7 bits.
  1331. APPEND_CHAR(c);
  1332. } else if (c <= 0x7ff) { // 11 bits
  1333. APPEND_CHAR(uint32_t(0xc0 | ((c >> 6) & 0x1f))); // Top 5 bits.
  1334. APPEND_CHAR(uint32_t(0x80 | (c & 0x3f))); // Bottom 6 bits.
  1335. } else if (c <= 0xffff) { // 16 bits
  1336. APPEND_CHAR(uint32_t(0xe0 | ((c >> 12) & 0x0f))); // Top 4 bits.
  1337. APPEND_CHAR(uint32_t(0x80 | ((c >> 6) & 0x3f))); // Middle 6 bits.
  1338. APPEND_CHAR(uint32_t(0x80 | (c & 0x3f))); // Bottom 6 bits.
  1339. } else if (c <= 0x001fffff) { // 21 bits
  1340. APPEND_CHAR(uint32_t(0xf0 | ((c >> 18) & 0x07))); // Top 3 bits.
  1341. APPEND_CHAR(uint32_t(0x80 | ((c >> 12) & 0x3f))); // Upper middle 6 bits.
  1342. APPEND_CHAR(uint32_t(0x80 | ((c >> 6) & 0x3f))); // Lower middle 6 bits.
  1343. APPEND_CHAR(uint32_t(0x80 | (c & 0x3f))); // Bottom 6 bits.
  1344. } else if (c <= 0x03ffffff) { // 26 bits
  1345. APPEND_CHAR(uint32_t(0xf8 | ((c >> 24) & 0x03))); // Top 2 bits.
  1346. APPEND_CHAR(uint32_t(0x80 | ((c >> 18) & 0x3f))); // Upper middle 6 bits.
  1347. APPEND_CHAR(uint32_t(0x80 | ((c >> 12) & 0x3f))); // middle 6 bits.
  1348. APPEND_CHAR(uint32_t(0x80 | ((c >> 6) & 0x3f))); // Lower middle 6 bits.
  1349. APPEND_CHAR(uint32_t(0x80 | (c & 0x3f))); // Bottom 6 bits.
  1350. } else if (c <= 0x7fffffff) { // 31 bits
  1351. APPEND_CHAR(uint32_t(0xfc | ((c >> 30) & 0x01))); // Top 1 bit.
  1352. APPEND_CHAR(uint32_t(0x80 | ((c >> 24) & 0x3f))); // Upper upper middle 6 bits.
  1353. APPEND_CHAR(uint32_t(0x80 | ((c >> 18) & 0x3f))); // Lower upper middle 6 bits.
  1354. APPEND_CHAR(uint32_t(0x80 | ((c >> 12) & 0x3f))); // Upper lower middle 6 bits.
  1355. APPEND_CHAR(uint32_t(0x80 | ((c >> 6) & 0x3f))); // Lower lower middle 6 bits.
  1356. APPEND_CHAR(uint32_t(0x80 | (c & 0x3f))); // Bottom 6 bits.
  1357. }
  1358. }
  1359. #undef APPEND_CHAR
  1360. *cdst = 0; //trailing zero
  1361. return utf8s;
  1362. }
  1363. String::String(const char *p_str) {
  1364. copy_from(p_str);
  1365. }
  1366. String::String(const CharType *p_str, int p_clip_to_len) {
  1367. copy_from(p_str, p_clip_to_len);
  1368. }
  1369. String::String(const StrRange &p_range) {
  1370. if (!p_range.c_str) {
  1371. return;
  1372. }
  1373. copy_from(p_range.c_str, p_range.len);
  1374. }
  1375. int String::hex_to_int(bool p_with_prefix) const {
  1376. int len = length();
  1377. if (len == 0 || (p_with_prefix && len < 3)) {
  1378. return 0;
  1379. }
  1380. const CharType *s = ptr();
  1381. int sign = s[0] == '-' ? -1 : 1;
  1382. if (sign < 0) {
  1383. s++;
  1384. }
  1385. if (p_with_prefix) {
  1386. if (s[0] != '0' || s[1] != 'x') {
  1387. return 0;
  1388. }
  1389. s += 2;
  1390. }
  1391. int hex = 0;
  1392. while (*s) {
  1393. CharType c = LOWERCASE(*s);
  1394. int n;
  1395. if (c >= '0' && c <= '9') {
  1396. n = c - '0';
  1397. } else if (c >= 'a' && c <= 'f') {
  1398. n = (c - 'a') + 10;
  1399. } else {
  1400. return 0;
  1401. }
  1402. // Check for overflow/underflow, with special case to ensure INT32_MIN does not result in error
  1403. bool overflow = ((hex > INT32_MAX / 16) && (sign == 1 || (sign == -1 && hex != (INT32_MAX >> 4) + 1))) || (sign == -1 && hex == (INT32_MAX >> 4) + 1 && c > '0');
  1404. ERR_FAIL_COND_V_MSG(overflow, sign == 1 ? INT32_MAX : INT32_MIN, "Cannot represent " + *this + " as integer, provided value is " + (sign == 1 ? "too big." : "too small."));
  1405. hex *= 16;
  1406. hex += n;
  1407. s++;
  1408. }
  1409. return hex * sign;
  1410. }
  1411. int64_t String::hex_to_int64(bool p_with_prefix) const {
  1412. if (p_with_prefix && length() < 3) {
  1413. return 0;
  1414. }
  1415. const CharType *s = ptr();
  1416. int64_t sign = s[0] == '-' ? -1 : 1;
  1417. if (sign < 0) {
  1418. s++;
  1419. }
  1420. if (p_with_prefix) {
  1421. if (s[0] != '0' || s[1] != 'x') {
  1422. return 0;
  1423. }
  1424. s += 2;
  1425. }
  1426. int64_t hex = 0;
  1427. while (*s) {
  1428. CharType c = LOWERCASE(*s);
  1429. int64_t n;
  1430. if (c >= '0' && c <= '9') {
  1431. n = c - '0';
  1432. } else if (c >= 'a' && c <= 'f') {
  1433. n = (c - 'a') + 10;
  1434. } else {
  1435. return 0;
  1436. }
  1437. bool overflow = ((hex > INT64_MAX / 16) && (sign == 1 || (sign == -1 && hex != (INT64_MAX >> 4) + 1))) || (sign == -1 && hex == (INT64_MAX >> 4) + 1 && c > '0');
  1438. ERR_FAIL_COND_V_MSG(overflow, sign == 1 ? INT64_MAX : INT64_MIN, "Cannot represent " + *this + " as 64-bit integer, provided value is " + (sign == 1 ? "too big." : "too small."));
  1439. hex *= 16;
  1440. hex += n;
  1441. s++;
  1442. }
  1443. return hex * sign;
  1444. }
  1445. int64_t String::bin_to_int64(bool p_with_prefix) const {
  1446. int len = length();
  1447. if (len == 0 || (p_with_prefix && len < 3)) {
  1448. return 0;
  1449. }
  1450. const CharType *s = ptr();
  1451. int64_t sign = s[0] == '-' ? -1 : 1;
  1452. if (sign < 0) {
  1453. s++;
  1454. }
  1455. if (p_with_prefix) {
  1456. if (s[0] != '0' || s[1] != 'b') {
  1457. return 0;
  1458. }
  1459. s += 2;
  1460. }
  1461. int64_t binary = 0;
  1462. while (*s) {
  1463. CharType c = LOWERCASE(*s);
  1464. int64_t n;
  1465. if (c == '0' || c == '1') {
  1466. n = c - '0';
  1467. } else {
  1468. return 0;
  1469. }
  1470. // Check for overflow/underflow, with special case to ensure INT64_MIN does not result in error
  1471. bool overflow = ((binary > INT64_MAX / 2) && (sign == 1 || (sign == -1 && binary != (INT64_MAX >> 1) + 1))) || (sign == -1 && binary == (INT64_MAX >> 1) + 1 && c > '0');
  1472. ERR_FAIL_COND_V_MSG(overflow, sign == 1 ? INT64_MAX : INT64_MIN, "Cannot represent " + *this + " as 64-bit integer, provided value is " + (sign == 1 ? "too big." : "too small."));
  1473. binary *= 2;
  1474. binary += n;
  1475. s++;
  1476. }
  1477. return binary * sign;
  1478. }
  1479. int String::to_int() const {
  1480. if (length() == 0) {
  1481. return 0;
  1482. }
  1483. int to = (find(".") >= 0) ? find(".") : length();
  1484. int integer = 0;
  1485. int sign = 1;
  1486. for (int i = 0; i < to; i++) {
  1487. CharType c = operator[](i);
  1488. if (c >= '0' && c <= '9') {
  1489. bool overflow = (integer > INT32_MAX / 10) || (integer == INT32_MAX / 10 && ((sign == 1 && c > '7') || (sign == -1 && c > '8')));
  1490. ERR_FAIL_COND_V_MSG(overflow, sign == 1 ? INT32_MAX : INT32_MIN, "Cannot represent " + *this + " as integer, provided value is " + (sign == 1 ? "too big." : "too small."));
  1491. integer *= 10;
  1492. integer += c - '0';
  1493. } else if (integer == 0 && c == '-') {
  1494. sign = -sign;
  1495. }
  1496. }
  1497. return integer * sign;
  1498. }
  1499. int64_t String::to_int64() const {
  1500. if (length() == 0) {
  1501. return 0;
  1502. }
  1503. int to = (find(".") >= 0) ? find(".") : length();
  1504. int64_t integer = 0;
  1505. int64_t sign = 1;
  1506. for (int i = 0; i < to; i++) {
  1507. CharType c = operator[](i);
  1508. if (c >= '0' && c <= '9') {
  1509. bool overflow = (integer > INT64_MAX / 10) || (integer == INT64_MAX / 10 && ((sign == 1 && c > '7') || (sign == -1 && c > '8')));
  1510. ERR_FAIL_COND_V_MSG(overflow, sign == 1 ? INT64_MAX : INT64_MIN, "Cannot represent " + *this + " as 64-bit integer, provided value is " + (sign == 1 ? "too big." : "too small."));
  1511. integer *= 10;
  1512. integer += c - '0';
  1513. } else if (integer == 0 && c == '-') {
  1514. sign = -sign;
  1515. }
  1516. }
  1517. return integer * sign;
  1518. }
  1519. int String::to_int(const char *p_str, int p_len) {
  1520. int to = 0;
  1521. if (p_len >= 0) {
  1522. to = p_len;
  1523. } else {
  1524. while (p_str[to] != 0 && p_str[to] != '.') {
  1525. to++;
  1526. }
  1527. }
  1528. int integer = 0;
  1529. int sign = 1;
  1530. for (int i = 0; i < to; i++) {
  1531. char c = p_str[i];
  1532. if (c >= '0' && c <= '9') {
  1533. bool overflow = (integer > INT32_MAX / 10) || (integer == INT32_MAX / 10 && ((sign == 1 && c > '7') || (sign == -1 && c > '8')));
  1534. ERR_FAIL_COND_V_MSG(overflow, sign == 1 ? INT32_MAX : INT32_MIN, "Cannot represent " + String(p_str).substr(0, to) + " as integer, provided value is " + (sign == 1 ? "too big." : "too small."));
  1535. integer *= 10;
  1536. integer += c - '0';
  1537. } else if (c == '-' && integer == 0) {
  1538. sign = -sign;
  1539. } else if (c != ' ') {
  1540. break;
  1541. }
  1542. }
  1543. return integer * sign;
  1544. }
  1545. bool String::is_numeric() const {
  1546. if (length() == 0) {
  1547. return false;
  1548. };
  1549. int s = 0;
  1550. if (operator[](0) == '-') {
  1551. ++s;
  1552. }
  1553. bool dot = false;
  1554. for (int i = s; i < length(); i++) {
  1555. CharType c = operator[](i);
  1556. if (c == '.') {
  1557. if (dot) {
  1558. return false;
  1559. };
  1560. dot = true;
  1561. }
  1562. if (c < '0' || c > '9') {
  1563. return false;
  1564. };
  1565. };
  1566. return true; // TODO: Use the parser below for this instead
  1567. };
  1568. template <class C>
  1569. static double built_in_strtod(
  1570. /* A decimal ASCII floating-point number,
  1571. * optionally preceded by white space. Must
  1572. * have form "-I.FE-X", where I is the integer
  1573. * part of the mantissa, F is the fractional
  1574. * part of the mantissa, and X is the
  1575. * exponent. Either of the signs may be "+",
  1576. * "-", or omitted. Either I or F may be
  1577. * omitted, or both. The decimal point isn't
  1578. * necessary unless F is present. The "E" may
  1579. * actually be an "e". E and X may both be
  1580. * omitted (but not just one). */
  1581. const C *string,
  1582. /* If non-nullptr, store terminating Cacter's
  1583. * address here. */
  1584. C **endPtr = nullptr) {
  1585. /* Largest possible base 10 exponent. Any
  1586. * exponent larger than this will already
  1587. * produce underflow or overflow, so there's
  1588. * no need to worry about additional digits. */
  1589. static const int maxExponent = 511;
  1590. /* Table giving binary powers of 10. Entry
  1591. * is 10^2^i. Used to convert decimal
  1592. * exponents into floating-point numbers. */
  1593. static const double powersOf10[] = {
  1594. 10.,
  1595. 100.,
  1596. 1.0e4,
  1597. 1.0e8,
  1598. 1.0e16,
  1599. 1.0e32,
  1600. 1.0e64,
  1601. 1.0e128,
  1602. 1.0e256
  1603. };
  1604. bool sign, expSign = false;
  1605. double fraction, dblExp;
  1606. const double *d;
  1607. const C *p;
  1608. int c;
  1609. /* Exponent read from "EX" field. */
  1610. int exp = 0;
  1611. /* Exponent that derives from the fractional
  1612. * part. Under normal circumstances, it is
  1613. * the negative of the number of digits in F.
  1614. * However, if I is very long, the last digits
  1615. * of I get dropped (otherwise a long I with a
  1616. * large negative exponent could cause an
  1617. * unnecessary overflow on I alone). In this
  1618. * case, fracExp is incremented one for each
  1619. * dropped digit. */
  1620. int fracExp = 0;
  1621. /* Number of digits in mantissa. */
  1622. int mantSize;
  1623. /* Number of mantissa digits BEFORE decimal point. */
  1624. int decPt;
  1625. /* Temporarily holds location of exponent in string. */
  1626. const C *pExp;
  1627. /*
  1628. * Strip off leading blanks and check for a sign.
  1629. */
  1630. p = string;
  1631. while (*p == ' ' || *p == '\t' || *p == '\n') {
  1632. p += 1;
  1633. }
  1634. if (*p == '-') {
  1635. sign = true;
  1636. p += 1;
  1637. } else {
  1638. if (*p == '+') {
  1639. p += 1;
  1640. }
  1641. sign = false;
  1642. }
  1643. /*
  1644. * Count the number of digits in the mantissa (including the decimal
  1645. * point), and also locate the decimal point.
  1646. */
  1647. decPt = -1;
  1648. for (mantSize = 0;; mantSize += 1) {
  1649. c = *p;
  1650. if (!IS_DIGIT(c)) {
  1651. if ((c != '.') || (decPt >= 0)) {
  1652. break;
  1653. }
  1654. decPt = mantSize;
  1655. }
  1656. p += 1;
  1657. }
  1658. /*
  1659. * Now suck up the digits in the mantissa. Use two integers to collect 9
  1660. * digits each (this is faster than using floating-point). If the mantissa
  1661. * has more than 18 digits, ignore the extras, since they can't affect the
  1662. * value anyway.
  1663. */
  1664. pExp = p;
  1665. p -= mantSize;
  1666. if (decPt < 0) {
  1667. decPt = mantSize;
  1668. } else {
  1669. mantSize -= 1; /* One of the digits was the point. */
  1670. }
  1671. if (mantSize > 18) {
  1672. fracExp = decPt - 18;
  1673. mantSize = 18;
  1674. } else {
  1675. fracExp = decPt - mantSize;
  1676. }
  1677. if (mantSize == 0) {
  1678. fraction = 0.0;
  1679. p = string;
  1680. goto done;
  1681. } else {
  1682. int frac1, frac2;
  1683. frac1 = 0;
  1684. for (; mantSize > 9; mantSize -= 1) {
  1685. c = *p;
  1686. p += 1;
  1687. if (c == '.') {
  1688. c = *p;
  1689. p += 1;
  1690. }
  1691. frac1 = 10 * frac1 + (c - '0');
  1692. }
  1693. frac2 = 0;
  1694. for (; mantSize > 0; mantSize -= 1) {
  1695. c = *p;
  1696. p += 1;
  1697. if (c == '.') {
  1698. c = *p;
  1699. p += 1;
  1700. }
  1701. frac2 = 10 * frac2 + (c - '0');
  1702. }
  1703. fraction = (1.0e9 * frac1) + frac2;
  1704. }
  1705. /*
  1706. * Skim off the exponent.
  1707. */
  1708. p = pExp;
  1709. if ((*p == 'E') || (*p == 'e')) {
  1710. p += 1;
  1711. if (*p == '-') {
  1712. expSign = true;
  1713. p += 1;
  1714. } else {
  1715. if (*p == '+') {
  1716. p += 1;
  1717. }
  1718. expSign = false;
  1719. }
  1720. if (!IS_DIGIT(CharType(*p))) {
  1721. p = pExp;
  1722. goto done;
  1723. }
  1724. while (IS_DIGIT(CharType(*p))) {
  1725. exp = exp * 10 + (*p - '0');
  1726. p += 1;
  1727. }
  1728. }
  1729. if (expSign) {
  1730. exp = fracExp - exp;
  1731. } else {
  1732. exp = fracExp + exp;
  1733. }
  1734. /*
  1735. * Generate a floating-point number that represents the exponent. Do this
  1736. * by processing the exponent one bit at a time to combine many powers of
  1737. * 2 of 10. Then combine the exponent with the fraction.
  1738. */
  1739. if (exp < 0) {
  1740. expSign = true;
  1741. exp = -exp;
  1742. } else {
  1743. expSign = false;
  1744. }
  1745. if (exp > maxExponent) {
  1746. exp = maxExponent;
  1747. WARN_PRINT("Exponent too high");
  1748. }
  1749. dblExp = 1.0;
  1750. for (d = powersOf10; exp != 0; exp >>= 1, ++d) {
  1751. if (exp & 01) {
  1752. dblExp *= *d;
  1753. }
  1754. }
  1755. if (expSign) {
  1756. fraction /= dblExp;
  1757. } else {
  1758. fraction *= dblExp;
  1759. }
  1760. done:
  1761. if (endPtr != nullptr) {
  1762. *endPtr = (C *)p;
  1763. }
  1764. if (sign) {
  1765. return -fraction;
  1766. }
  1767. return fraction;
  1768. }
  1769. #define READING_SIGN 0
  1770. #define READING_INT 1
  1771. #define READING_DEC 2
  1772. #define READING_EXP 3
  1773. #define READING_DONE 4
  1774. double String::to_double(const char *p_str) {
  1775. #ifndef NO_USE_STDLIB
  1776. return built_in_strtod<char>(p_str);
  1777. #else
  1778. return built_in_strtod<char>(p_str);
  1779. #endif
  1780. }
  1781. float String::to_float() const {
  1782. return to_double();
  1783. }
  1784. double String::to_double(const CharType *p_str, const CharType **r_end) {
  1785. return built_in_strtod<CharType>(p_str, (CharType **)r_end);
  1786. }
  1787. int64_t String::to_int(const CharType *p_str, int p_len) {
  1788. if (p_len == 0 || !p_str[0]) {
  1789. return 0;
  1790. }
  1791. ///@todo make more exact so saving and loading does not lose precision
  1792. int64_t integer = 0;
  1793. int64_t sign = 1;
  1794. int reading = READING_SIGN;
  1795. const CharType *str = p_str;
  1796. const CharType *limit = &p_str[p_len];
  1797. while (*str && reading != READING_DONE && str != limit) {
  1798. CharType c = *(str++);
  1799. switch (reading) {
  1800. case READING_SIGN: {
  1801. if (c >= '0' && c <= '9') {
  1802. reading = READING_INT;
  1803. // let it fallthrough
  1804. } else if (c == '-') {
  1805. sign = -1;
  1806. reading = READING_INT;
  1807. break;
  1808. } else if (c == '+') {
  1809. sign = 1;
  1810. reading = READING_INT;
  1811. break;
  1812. } else {
  1813. break;
  1814. }
  1815. }
  1816. case READING_INT: {
  1817. if (c >= '0' && c <= '9') {
  1818. if (integer > INT64_MAX / 10) {
  1819. String number("");
  1820. str = p_str;
  1821. while (*str && str != limit) {
  1822. number += *(str++);
  1823. }
  1824. ERR_FAIL_V_MSG(sign == 1 ? INT64_MAX : INT64_MIN, "Cannot represent " + number + " as integer, provided value is " + (sign == 1 ? "too big." : "too small."));
  1825. }
  1826. integer *= 10;
  1827. integer += c - '0';
  1828. } else {
  1829. reading = READING_DONE;
  1830. }
  1831. } break;
  1832. }
  1833. }
  1834. return sign * integer;
  1835. }
  1836. double String::to_double() const {
  1837. if (empty()) {
  1838. return 0;
  1839. }
  1840. #ifndef NO_USE_STDLIB
  1841. return built_in_strtod<CharType>(c_str());
  1842. //return wcstod(c_str(),NULL); DOES NOT WORK ON ANDROID :(
  1843. #else
  1844. return built_in_strtod<CharType>(c_str());
  1845. #endif
  1846. }
  1847. bool operator==(const char *p_chr, const String &p_str) {
  1848. return p_str == p_chr;
  1849. }
  1850. String operator+(const char *p_chr, const String &p_str) {
  1851. String tmp = p_chr;
  1852. tmp += p_str;
  1853. return tmp;
  1854. }
  1855. String operator+(CharType p_chr, const String &p_str) {
  1856. return (String::chr(p_chr) + p_str);
  1857. }
  1858. uint32_t String::hash(const char *p_cstr) {
  1859. uint32_t hashv = 5381;
  1860. uint32_t c;
  1861. while ((c = *p_cstr++)) {
  1862. hashv = ((hashv << 5) + hashv) + c; /* hash * 33 + c */
  1863. }
  1864. return hashv;
  1865. }
  1866. uint32_t String::hash(const char *p_cstr, int p_len) {
  1867. uint32_t hashv = 5381;
  1868. for (int i = 0; i < p_len; i++) {
  1869. hashv = ((hashv << 5) + hashv) + p_cstr[i]; /* hash * 33 + c */
  1870. }
  1871. return hashv;
  1872. }
  1873. uint32_t String::hash(const CharType *p_cstr, int p_len) {
  1874. uint32_t hashv = 5381;
  1875. for (int i = 0; i < p_len; i++) {
  1876. hashv = ((hashv << 5) + hashv) + p_cstr[i]; /* hash * 33 + c */
  1877. }
  1878. return hashv;
  1879. }
  1880. uint32_t String::hash(const CharType *p_cstr) {
  1881. uint32_t hashv = 5381;
  1882. uint32_t c;
  1883. while ((c = *p_cstr++)) {
  1884. hashv = ((hashv << 5) + hashv) + c; /* hash * 33 + c */
  1885. }
  1886. return hashv;
  1887. }
  1888. uint32_t String::hash() const {
  1889. /* simple djb2 hashing */
  1890. const CharType *chr = c_str();
  1891. uint32_t hashv = 5381;
  1892. uint32_t c;
  1893. while ((c = *chr++)) {
  1894. hashv = ((hashv << 5) + hashv) + c; /* hash * 33 + c */
  1895. }
  1896. return hashv;
  1897. }
  1898. uint64_t String::hash64() const {
  1899. /* simple djb2 hashing */
  1900. const CharType *chr = c_str();
  1901. uint64_t hashv = 5381;
  1902. uint64_t c;
  1903. while ((c = *chr++)) {
  1904. hashv = ((hashv << 5) + hashv) + c; /* hash * 33 + c */
  1905. }
  1906. return hashv;
  1907. }
  1908. String String::md5_text() const {
  1909. CharString cs = utf8();
  1910. unsigned char hash[16];
  1911. CryptoCore::md5((unsigned char *)cs.ptr(), cs.length(), hash);
  1912. return String::hex_encode_buffer(hash, 16);
  1913. }
  1914. String String::sha1_text() const {
  1915. CharString cs = utf8();
  1916. unsigned char hash[20];
  1917. CryptoCore::sha1((unsigned char *)cs.ptr(), cs.length(), hash);
  1918. return String::hex_encode_buffer(hash, 20);
  1919. }
  1920. String String::sha256_text() const {
  1921. CharString cs = utf8();
  1922. unsigned char hash[32];
  1923. CryptoCore::sha256((unsigned char *)cs.ptr(), cs.length(), hash);
  1924. return String::hex_encode_buffer(hash, 32);
  1925. }
  1926. Vector<uint8_t> String::md5_buffer() const {
  1927. CharString cs = utf8();
  1928. unsigned char hash[16];
  1929. CryptoCore::md5((unsigned char *)cs.ptr(), cs.length(), hash);
  1930. Vector<uint8_t> ret;
  1931. ret.resize(16);
  1932. for (int i = 0; i < 16; i++) {
  1933. ret.write[i] = hash[i];
  1934. }
  1935. return ret;
  1936. };
  1937. Vector<uint8_t> String::sha1_buffer() const {
  1938. CharString cs = utf8();
  1939. unsigned char hash[20];
  1940. CryptoCore::sha1((unsigned char *)cs.ptr(), cs.length(), hash);
  1941. Vector<uint8_t> ret;
  1942. ret.resize(20);
  1943. for (int i = 0; i < 20; i++) {
  1944. ret.write[i] = hash[i];
  1945. }
  1946. return ret;
  1947. }
  1948. Vector<uint8_t> String::sha256_buffer() const {
  1949. CharString cs = utf8();
  1950. unsigned char hash[32];
  1951. CryptoCore::sha256((unsigned char *)cs.ptr(), cs.length(), hash);
  1952. Vector<uint8_t> ret;
  1953. ret.resize(32);
  1954. for (int i = 0; i < 32; i++) {
  1955. ret.write[i] = hash[i];
  1956. }
  1957. return ret;
  1958. }
  1959. String String::insert(int p_at_pos, const String &p_string) const {
  1960. if (p_at_pos < 0) {
  1961. return *this;
  1962. }
  1963. if (p_at_pos > length()) {
  1964. p_at_pos = length();
  1965. }
  1966. String pre;
  1967. if (p_at_pos > 0) {
  1968. pre = substr(0, p_at_pos);
  1969. }
  1970. String post;
  1971. if (p_at_pos < length()) {
  1972. post = substr(p_at_pos, length() - p_at_pos);
  1973. }
  1974. return pre + p_string + post;
  1975. }
  1976. String String::substr(int p_from, int p_chars) const {
  1977. if (p_chars == -1) {
  1978. p_chars = length() - p_from;
  1979. }
  1980. if (empty() || p_from < 0 || p_from >= length() || p_chars <= 0) {
  1981. return "";
  1982. }
  1983. if ((p_from + p_chars) > length()) {
  1984. p_chars = length() - p_from;
  1985. }
  1986. if (p_from == 0 && p_chars >= length()) {
  1987. return String(*this);
  1988. }
  1989. String s = String();
  1990. s.copy_from_unchecked(&c_str()[p_from], p_chars);
  1991. return s;
  1992. }
  1993. int String::find_last(const String &p_str) const {
  1994. int pos = -1;
  1995. int findfrom = 0;
  1996. int findres = -1;
  1997. while ((findres = find(p_str, findfrom)) != -1) {
  1998. pos = findres;
  1999. findfrom = pos + 1;
  2000. }
  2001. return pos;
  2002. }
  2003. int String::find(const String &p_str, int p_from) const {
  2004. if (p_from < 0) {
  2005. return -1;
  2006. }
  2007. const int src_len = p_str.length();
  2008. const int len = length();
  2009. if (src_len == 0 || len == 0) {
  2010. return -1; // won't find anything!
  2011. }
  2012. const CharType *src = c_str();
  2013. const CharType *str = p_str.c_str();
  2014. for (int i = p_from; i <= (len - src_len); i++) {
  2015. bool found = true;
  2016. for (int j = 0; j < src_len; j++) {
  2017. int read_pos = i + j;
  2018. if (read_pos >= len) {
  2019. ERR_PRINT("read_pos>=len");
  2020. return -1;
  2021. };
  2022. if (src[read_pos] != str[j]) {
  2023. found = false;
  2024. break;
  2025. }
  2026. }
  2027. if (found) {
  2028. return i;
  2029. }
  2030. }
  2031. return -1;
  2032. }
  2033. int String::find(const char *p_str, int p_from) const {
  2034. if (p_from < 0) {
  2035. return -1;
  2036. }
  2037. const int len = length();
  2038. if (len == 0) {
  2039. return -1; // won't find anything!
  2040. }
  2041. const CharType *src = c_str();
  2042. int src_len = 0;
  2043. while (p_str[src_len] != '\0') {
  2044. src_len++;
  2045. }
  2046. if (src_len == 1) {
  2047. const char needle = p_str[0];
  2048. for (int i = p_from; i < len; i++) {
  2049. if (src[i] == needle) {
  2050. return i;
  2051. }
  2052. }
  2053. } else {
  2054. for (int i = p_from; i <= (len - src_len); i++) {
  2055. bool found = true;
  2056. for (int j = 0; j < src_len; j++) {
  2057. int read_pos = i + j;
  2058. if (read_pos >= len) {
  2059. ERR_PRINT("read_pos>=len");
  2060. return -1;
  2061. };
  2062. if (src[read_pos] != p_str[j]) {
  2063. found = false;
  2064. break;
  2065. }
  2066. }
  2067. if (found) {
  2068. return i;
  2069. }
  2070. }
  2071. }
  2072. return -1;
  2073. }
  2074. int String::find_char(const CharType &p_char, int p_from) const {
  2075. return _cowdata.find(p_char, p_from);
  2076. }
  2077. int String::findmk(const Vector<String> &p_keys, int p_from, int *r_key) const {
  2078. if (p_from < 0) {
  2079. return -1;
  2080. }
  2081. if (p_keys.size() == 0) {
  2082. return -1;
  2083. }
  2084. //int src_len=p_str.length();
  2085. const String *keys = &p_keys[0];
  2086. int key_count = p_keys.size();
  2087. int len = length();
  2088. if (len == 0) {
  2089. return -1; // won't find anything!
  2090. }
  2091. const CharType *src = c_str();
  2092. for (int i = p_from; i < len; i++) {
  2093. bool found = true;
  2094. for (int k = 0; k < key_count; k++) {
  2095. found = true;
  2096. if (r_key) {
  2097. *r_key = k;
  2098. }
  2099. const CharType *cmp = keys[k].c_str();
  2100. int l = keys[k].length();
  2101. for (int j = 0; j < l; j++) {
  2102. int read_pos = i + j;
  2103. if (read_pos >= len) {
  2104. found = false;
  2105. break;
  2106. };
  2107. if (src[read_pos] != cmp[j]) {
  2108. found = false;
  2109. break;
  2110. }
  2111. }
  2112. if (found) {
  2113. break;
  2114. }
  2115. }
  2116. if (found) {
  2117. return i;
  2118. }
  2119. }
  2120. return -1;
  2121. }
  2122. int String::findn(const String &p_str, int p_from) const {
  2123. if (p_from < 0) {
  2124. return -1;
  2125. }
  2126. int src_len = p_str.length();
  2127. if (src_len == 0 || length() == 0) {
  2128. return -1; // won't find anything!
  2129. }
  2130. const CharType *srcd = c_str();
  2131. for (int i = p_from; i <= (length() - src_len); i++) {
  2132. bool found = true;
  2133. for (int j = 0; j < src_len; j++) {
  2134. int read_pos = i + j;
  2135. if (read_pos >= length()) {
  2136. ERR_PRINT("read_pos>=length()");
  2137. return -1;
  2138. };
  2139. CharType src = _find_lower(srcd[read_pos]);
  2140. CharType dst = _find_lower(p_str[j]);
  2141. if (src != dst) {
  2142. found = false;
  2143. break;
  2144. }
  2145. }
  2146. if (found) {
  2147. return i;
  2148. }
  2149. }
  2150. return -1;
  2151. }
  2152. int String::rfind(const String &p_str, int p_from) const {
  2153. // establish a limit
  2154. int limit = length() - p_str.length();
  2155. if (limit < 0) {
  2156. return -1;
  2157. }
  2158. // establish a starting point
  2159. if (p_from < 0) {
  2160. p_from = limit;
  2161. } else if (p_from > limit) {
  2162. p_from = limit;
  2163. }
  2164. int src_len = p_str.length();
  2165. int len = length();
  2166. if (src_len == 0 || len == 0) {
  2167. return -1; // won't find anything!
  2168. }
  2169. const CharType *src = c_str();
  2170. for (int i = p_from; i >= 0; i--) {
  2171. bool found = true;
  2172. for (int j = 0; j < src_len; j++) {
  2173. int read_pos = i + j;
  2174. if (read_pos >= len) {
  2175. ERR_PRINT("read_pos>=len");
  2176. return -1;
  2177. };
  2178. if (src[read_pos] != p_str[j]) {
  2179. found = false;
  2180. break;
  2181. }
  2182. }
  2183. if (found) {
  2184. return i;
  2185. }
  2186. }
  2187. return -1;
  2188. }
  2189. int String::rfindn(const String &p_str, int p_from) const {
  2190. // establish a limit
  2191. int limit = length() - p_str.length();
  2192. if (limit < 0) {
  2193. return -1;
  2194. }
  2195. // establish a starting point
  2196. if (p_from < 0) {
  2197. p_from = limit;
  2198. } else if (p_from > limit) {
  2199. p_from = limit;
  2200. }
  2201. int src_len = p_str.length();
  2202. int len = length();
  2203. if (src_len == 0 || len == 0) {
  2204. return -1; // won't find anything!
  2205. }
  2206. const CharType *src = c_str();
  2207. for (int i = p_from; i >= 0; i--) {
  2208. bool found = true;
  2209. for (int j = 0; j < src_len; j++) {
  2210. int read_pos = i + j;
  2211. if (read_pos >= len) {
  2212. ERR_PRINT("read_pos>=len");
  2213. return -1;
  2214. };
  2215. CharType srcc = _find_lower(src[read_pos]);
  2216. CharType dstc = _find_lower(p_str[j]);
  2217. if (srcc != dstc) {
  2218. found = false;
  2219. break;
  2220. }
  2221. }
  2222. if (found) {
  2223. return i;
  2224. }
  2225. }
  2226. return -1;
  2227. }
  2228. bool String::ends_with(const String &p_string) const {
  2229. int l = p_string.length();
  2230. if (l > length()) {
  2231. return false;
  2232. }
  2233. if (l == 0) {
  2234. return true;
  2235. }
  2236. const CharType *p = &p_string[0];
  2237. const CharType *s = &operator[](length() - l);
  2238. for (int i = 0; i < l; i++) {
  2239. if (p[i] != s[i]) {
  2240. return false;
  2241. }
  2242. }
  2243. return true;
  2244. }
  2245. bool String::begins_with(const String &p_string) const {
  2246. int l = p_string.length();
  2247. if (l > length()) {
  2248. return false;
  2249. }
  2250. if (l == 0) {
  2251. return true;
  2252. }
  2253. const CharType *p = &p_string[0];
  2254. const CharType *s = &operator[](0);
  2255. for (int i = 0; i < l; i++) {
  2256. if (p[i] != s[i]) {
  2257. return false;
  2258. }
  2259. }
  2260. return true;
  2261. }
  2262. bool String::begins_with(const char *p_string) const {
  2263. int l = length();
  2264. if (l == 0 || !p_string) {
  2265. return false;
  2266. }
  2267. const CharType *str = &operator[](0);
  2268. int i = 0;
  2269. while (*p_string && i < l) {
  2270. if (*p_string != str[i]) {
  2271. return false;
  2272. }
  2273. i++;
  2274. p_string++;
  2275. }
  2276. return *p_string == 0;
  2277. }
  2278. bool String::is_enclosed_in(const String &p_string) const {
  2279. return begins_with(p_string) && ends_with(p_string);
  2280. }
  2281. bool String::is_subsequence_of(const String &p_string) const {
  2282. return _base_is_subsequence_of(p_string, false);
  2283. }
  2284. bool String::is_subsequence_ofi(const String &p_string) const {
  2285. return _base_is_subsequence_of(p_string, true);
  2286. }
  2287. bool String::is_quoted() const {
  2288. return is_enclosed_in("\"") || is_enclosed_in("'");
  2289. }
  2290. int String::_count(const String &p_string, int p_from, int p_to, bool p_case_insensitive) const {
  2291. if (p_string.empty()) {
  2292. return 0;
  2293. }
  2294. int len = length();
  2295. int slen = p_string.length();
  2296. if (len < slen) {
  2297. return 0;
  2298. }
  2299. String str;
  2300. if (p_from >= 0 && p_to >= 0) {
  2301. if (p_to == 0) {
  2302. p_to = len;
  2303. } else if (p_from >= p_to) {
  2304. return 0;
  2305. }
  2306. if (p_from == 0 && p_to == len) {
  2307. str = String();
  2308. str.copy_from_unchecked(&c_str()[0], len);
  2309. } else {
  2310. str = substr(p_from, p_to - p_from);
  2311. }
  2312. } else {
  2313. return 0;
  2314. }
  2315. int c = 0;
  2316. int idx = -1;
  2317. do {
  2318. idx = p_case_insensitive ? str.findn(p_string) : str.find(p_string);
  2319. if (idx != -1) {
  2320. str = str.substr(idx + slen, str.length() - slen);
  2321. ++c;
  2322. }
  2323. } while (idx != -1);
  2324. return c;
  2325. }
  2326. int String::count(const String &p_string, int p_from, int p_to) const {
  2327. return _count(p_string, p_from, p_to, false);
  2328. }
  2329. int String::countn(const String &p_string, int p_from, int p_to) const {
  2330. return _count(p_string, p_from, p_to, true);
  2331. }
  2332. bool String::_base_is_subsequence_of(const String &p_string, bool case_insensitive) const {
  2333. int len = length();
  2334. if (len == 0) {
  2335. // Technically an empty string is subsequence of any string
  2336. return true;
  2337. }
  2338. if (len > p_string.length()) {
  2339. return false;
  2340. }
  2341. const CharType *src = &operator[](0);
  2342. const CharType *tgt = &p_string[0];
  2343. for (; *src && *tgt; tgt++) {
  2344. bool match = false;
  2345. if (case_insensitive) {
  2346. CharType srcc = _find_lower(*src);
  2347. CharType tgtc = _find_lower(*tgt);
  2348. match = srcc == tgtc;
  2349. } else {
  2350. match = *src == *tgt;
  2351. }
  2352. if (match) {
  2353. src++;
  2354. if (!*src) {
  2355. return true;
  2356. }
  2357. }
  2358. }
  2359. return false;
  2360. }
  2361. Vector<String> String::bigrams() const {
  2362. int n_pairs = length() - 1;
  2363. Vector<String> b;
  2364. if (n_pairs <= 0) {
  2365. return b;
  2366. }
  2367. b.resize(n_pairs);
  2368. for (int i = 0; i < n_pairs; i++) {
  2369. b.write[i] = substr(i, 2);
  2370. }
  2371. return b;
  2372. }
  2373. // Similarity according to Sorensen-Dice coefficient
  2374. float String::similarity(const String &p_string) const {
  2375. if (operator==(p_string)) {
  2376. // Equal strings are totally similar
  2377. return 1.0f;
  2378. }
  2379. if (length() < 2 || p_string.length() < 2) {
  2380. // No way to calculate similarity without a single bigram
  2381. return 0.0f;
  2382. }
  2383. Vector<String> src_bigrams = bigrams();
  2384. Vector<String> tgt_bigrams = p_string.bigrams();
  2385. int src_size = src_bigrams.size();
  2386. int tgt_size = tgt_bigrams.size();
  2387. float sum = src_size + tgt_size;
  2388. float inter = 0;
  2389. for (int i = 0; i < src_size; i++) {
  2390. for (int j = 0; j < tgt_size; j++) {
  2391. if (src_bigrams[i] == tgt_bigrams[j]) {
  2392. inter++;
  2393. break;
  2394. }
  2395. }
  2396. }
  2397. return (2.0f * inter) / sum;
  2398. }
  2399. static bool _wildcard_match(const CharType *p_pattern, const CharType *p_string, bool p_case_sensitive) {
  2400. switch (*p_pattern) {
  2401. case '\0':
  2402. return !*p_string;
  2403. case '*':
  2404. return _wildcard_match(p_pattern + 1, p_string, p_case_sensitive) || (*p_string && _wildcard_match(p_pattern, p_string + 1, p_case_sensitive));
  2405. case '?':
  2406. return *p_string && (*p_string != '.') && _wildcard_match(p_pattern + 1, p_string + 1, p_case_sensitive);
  2407. default:
  2408. return (p_case_sensitive ? (*p_string == *p_pattern) : (_find_upper(*p_string) == _find_upper(*p_pattern))) && _wildcard_match(p_pattern + 1, p_string + 1, p_case_sensitive);
  2409. }
  2410. }
  2411. bool String::match(const String &p_wildcard) const {
  2412. if (!p_wildcard.length() || !length()) {
  2413. return false;
  2414. }
  2415. return _wildcard_match(p_wildcard.c_str(), c_str(), true);
  2416. }
  2417. bool String::matchn(const String &p_wildcard) const {
  2418. if (!p_wildcard.length() || !length()) {
  2419. return false;
  2420. }
  2421. return _wildcard_match(p_wildcard.c_str(), c_str(), false);
  2422. }
  2423. String String::format(const Variant &values, String placeholder) const {
  2424. String new_string = String(this->ptr());
  2425. if (values.get_type() == Variant::ARRAY) {
  2426. Array values_arr = values;
  2427. for (int i = 0; i < values_arr.size(); i++) {
  2428. String i_as_str = String::num_int64(i);
  2429. if (values_arr[i].get_type() == Variant::ARRAY) { //Array in Array structure [["name","RobotGuy"],[0,"godot"],["strength",9000.91]]
  2430. Array value_arr = values_arr[i];
  2431. if (value_arr.size() == 2) {
  2432. Variant v_key = value_arr[0];
  2433. String key = v_key;
  2434. Variant v_val = value_arr[1];
  2435. String val = v_val;
  2436. new_string = new_string.replace(placeholder.replace("_", key), val);
  2437. } else {
  2438. ERR_PRINT(String("STRING.format Inner Array size != 2 ").ascii().get_data());
  2439. }
  2440. } else { //Array structure ["RobotGuy","Logis","rookie"]
  2441. Variant v_val = values_arr[i];
  2442. String val = v_val;
  2443. if (placeholder.find("_") > -1) {
  2444. new_string = new_string.replace(placeholder.replace("_", i_as_str), val);
  2445. } else {
  2446. new_string = new_string.replace_first(placeholder, val);
  2447. }
  2448. }
  2449. }
  2450. } else if (values.get_type() == Variant::DICTIONARY) {
  2451. Dictionary d = values;
  2452. List<Variant> keys;
  2453. d.get_key_list(&keys);
  2454. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  2455. String key = E->get();
  2456. String val = d[E->get()];
  2457. new_string = new_string.replace(placeholder.replace("_", key), val);
  2458. }
  2459. } else {
  2460. ERR_PRINT(String("Invalid type: use Array or Dictionary.").ascii().get_data());
  2461. }
  2462. return new_string;
  2463. }
  2464. String String::replace(const String &p_key, const String &p_with) const {
  2465. String new_string;
  2466. int search_from = 0;
  2467. int result = 0;
  2468. while ((result = find(p_key, search_from)) >= 0) {
  2469. new_string += substr(search_from, result - search_from);
  2470. new_string += p_with;
  2471. search_from = result + p_key.length();
  2472. }
  2473. if (search_from == 0) {
  2474. return *this;
  2475. }
  2476. new_string += substr(search_from, length() - search_from);
  2477. return new_string;
  2478. }
  2479. String String::replace(const char *p_key, const char *p_with) const {
  2480. String new_string;
  2481. int search_from = 0;
  2482. int result = 0;
  2483. while ((result = find(p_key, search_from)) >= 0) {
  2484. new_string += substr(search_from, result - search_from);
  2485. new_string += p_with;
  2486. int k = 0;
  2487. while (p_key[k] != '\0') {
  2488. k++;
  2489. }
  2490. search_from = result + k;
  2491. }
  2492. if (search_from == 0) {
  2493. return *this;
  2494. }
  2495. new_string += substr(search_from, length() - search_from);
  2496. return new_string;
  2497. }
  2498. String String::replace_first(const String &p_key, const String &p_with) const {
  2499. int pos = find(p_key);
  2500. if (pos >= 0) {
  2501. return substr(0, pos) + p_with + substr(pos + p_key.length(), length());
  2502. }
  2503. return *this;
  2504. }
  2505. String String::replacen(const String &p_key, const String &p_with) const {
  2506. String new_string;
  2507. int search_from = 0;
  2508. int result = 0;
  2509. while ((result = findn(p_key, search_from)) >= 0) {
  2510. new_string += substr(search_from, result - search_from);
  2511. new_string += p_with;
  2512. search_from = result + p_key.length();
  2513. }
  2514. if (search_from == 0) {
  2515. return *this;
  2516. }
  2517. new_string += substr(search_from, length() - search_from);
  2518. return new_string;
  2519. }
  2520. String String::repeat(int p_count) const {
  2521. ERR_FAIL_COND_V_MSG(p_count < 0, "", "Parameter count should be a positive number.");
  2522. String new_string;
  2523. const CharType *src = this->c_str();
  2524. new_string.resize(length() * p_count + 1);
  2525. new_string[length() * p_count] = 0;
  2526. for (int i = 0; i < p_count; i++) {
  2527. for (int j = 0; j < length(); j++) {
  2528. new_string[i * length() + j] = src[j];
  2529. }
  2530. }
  2531. return new_string;
  2532. }
  2533. String String::left(int p_pos) const {
  2534. if (p_pos <= 0) {
  2535. return "";
  2536. }
  2537. if (p_pos >= length()) {
  2538. return *this;
  2539. }
  2540. return substr(0, p_pos);
  2541. }
  2542. String String::right(int p_pos) const {
  2543. if (p_pos >= length()) {
  2544. return "";
  2545. }
  2546. if (p_pos <= 0) {
  2547. return *this;
  2548. }
  2549. return substr(p_pos, (length() - p_pos));
  2550. }
  2551. CharType String::ord_at(int p_idx) const {
  2552. ERR_FAIL_INDEX_V(p_idx, length(), 0);
  2553. return operator[](p_idx);
  2554. }
  2555. String String::indent(const String &p_prefix) const {
  2556. String new_string;
  2557. int line_start = 0;
  2558. for (int i = 0; i < length(); i++) {
  2559. const char32_t c = operator[](i);
  2560. if (c == '\n') {
  2561. if (i == line_start) {
  2562. new_string += c; // Leave empty lines empty.
  2563. } else {
  2564. new_string += p_prefix + substr(line_start, i - line_start + 1);
  2565. }
  2566. line_start = i + 1;
  2567. }
  2568. }
  2569. if (line_start != length()) {
  2570. new_string += p_prefix + substr(line_start);
  2571. }
  2572. return new_string;
  2573. }
  2574. String String::dedent() const {
  2575. String new_string;
  2576. String indent;
  2577. bool has_indent = false;
  2578. bool has_text = false;
  2579. int line_start = 0;
  2580. int indent_stop = -1;
  2581. for (int i = 0; i < length(); i++) {
  2582. CharType c = operator[](i);
  2583. if (c == '\n') {
  2584. if (has_text) {
  2585. new_string += substr(indent_stop, i - indent_stop);
  2586. }
  2587. new_string += "\n";
  2588. has_text = false;
  2589. line_start = i + 1;
  2590. indent_stop = -1;
  2591. } else if (!has_text) {
  2592. if (c > 32) {
  2593. has_text = true;
  2594. if (!has_indent) {
  2595. has_indent = true;
  2596. indent = substr(line_start, i - line_start);
  2597. indent_stop = i;
  2598. }
  2599. }
  2600. if (has_indent && indent_stop < 0) {
  2601. int j = i - line_start;
  2602. if (j >= indent.length() || c != indent[j]) {
  2603. indent_stop = i;
  2604. }
  2605. }
  2606. }
  2607. }
  2608. if (has_text) {
  2609. new_string += substr(indent_stop, length() - indent_stop);
  2610. }
  2611. return new_string;
  2612. }
  2613. String String::strip_edges(bool left, bool right) const {
  2614. int len = length();
  2615. int beg = 0, end = len;
  2616. if (left) {
  2617. for (int i = 0; i < len; i++) {
  2618. if (operator[](i) <= 32) {
  2619. beg++;
  2620. } else {
  2621. break;
  2622. }
  2623. }
  2624. }
  2625. if (right) {
  2626. for (int i = (int)(len - 1); i >= 0; i--) {
  2627. if (operator[](i) <= 32) {
  2628. end--;
  2629. } else {
  2630. break;
  2631. }
  2632. }
  2633. }
  2634. if (beg == 0 && end == len) {
  2635. return *this;
  2636. }
  2637. return substr(beg, end - beg);
  2638. }
  2639. String String::strip_escapes() const {
  2640. String new_string;
  2641. for (int i = 0; i < length(); i++) {
  2642. // Escape characters on first page of the ASCII table, before 32 (Space).
  2643. if (operator[](i) < 32) {
  2644. continue;
  2645. }
  2646. new_string += operator[](i);
  2647. }
  2648. return new_string;
  2649. }
  2650. String String::lstrip(const String &p_chars) const {
  2651. int len = length();
  2652. int beg;
  2653. for (beg = 0; beg < len; beg++) {
  2654. if (p_chars.find_char(get(beg)) == -1) {
  2655. break;
  2656. }
  2657. }
  2658. if (beg == 0) {
  2659. return *this;
  2660. }
  2661. return substr(beg, len - beg);
  2662. }
  2663. String String::rstrip(const String &p_chars) const {
  2664. int len = length();
  2665. int end;
  2666. for (end = len - 1; end >= 0; end--) {
  2667. if (p_chars.find_char(get(end)) == -1) {
  2668. break;
  2669. }
  2670. }
  2671. if (end == len - 1) {
  2672. return *this;
  2673. }
  2674. return substr(0, end + 1);
  2675. }
  2676. String String::simplify_path() const {
  2677. String s = *this;
  2678. String drive;
  2679. if (s.begins_with("local://")) {
  2680. drive = "local://";
  2681. s = s.substr(8, s.length());
  2682. } else if (s.begins_with("res://")) {
  2683. drive = "res://";
  2684. s = s.substr(6, s.length());
  2685. } else if (s.begins_with("user://")) {
  2686. drive = "user://";
  2687. s = s.substr(7, s.length());
  2688. } else if (s.begins_with("/") || s.begins_with("\\")) {
  2689. drive = s.substr(0, 1);
  2690. s = s.substr(1, s.length() - 1);
  2691. } else {
  2692. int p = s.find(":/");
  2693. if (p == -1) {
  2694. p = s.find(":\\");
  2695. }
  2696. if (p != -1 && p < s.find("/")) {
  2697. drive = s.substr(0, p + 2);
  2698. s = s.substr(p + 2, s.length());
  2699. }
  2700. }
  2701. s = s.replace("\\", "/");
  2702. while (true) { // in case of using 2 or more slash
  2703. String compare = s.replace("//", "/");
  2704. if (s == compare) {
  2705. break;
  2706. } else {
  2707. s = compare;
  2708. }
  2709. }
  2710. Vector<String> dirs = s.split("/", false);
  2711. for (int i = 0; i < dirs.size(); i++) {
  2712. String d = dirs[i];
  2713. if (d == ".") {
  2714. dirs.remove(i);
  2715. i--;
  2716. } else if (d == "..") {
  2717. if (i == 0) {
  2718. dirs.remove(i);
  2719. i--;
  2720. } else {
  2721. dirs.remove(i);
  2722. dirs.remove(i - 1);
  2723. i -= 2;
  2724. }
  2725. }
  2726. }
  2727. s = "";
  2728. for (int i = 0; i < dirs.size(); i++) {
  2729. if (i > 0) {
  2730. s += "/";
  2731. }
  2732. s += dirs[i];
  2733. }
  2734. return drive + s;
  2735. }
  2736. static int _humanize_digits(int p_num) {
  2737. if (p_num < 100) {
  2738. return 2;
  2739. } else if (p_num < 1024) {
  2740. return 1;
  2741. } else {
  2742. return 0;
  2743. }
  2744. }
  2745. String String::humanize_size(uint64_t p_size) {
  2746. uint64_t _div = 1;
  2747. Vector<String> prefixes;
  2748. prefixes.push_back(RTR("B"));
  2749. prefixes.push_back(RTR("KiB"));
  2750. prefixes.push_back(RTR("MiB"));
  2751. prefixes.push_back(RTR("GiB"));
  2752. prefixes.push_back(RTR("TiB"));
  2753. prefixes.push_back(RTR("PiB"));
  2754. prefixes.push_back(RTR("EiB"));
  2755. int prefix_idx = 0;
  2756. while (prefix_idx < prefixes.size() - 1 && p_size > (_div * 1024)) {
  2757. _div *= 1024;
  2758. prefix_idx++;
  2759. }
  2760. const int digits = prefix_idx > 0 ? _humanize_digits(p_size / _div) : 0;
  2761. const double divisor = prefix_idx > 0 ? _div : 1;
  2762. return String::num(p_size / divisor).pad_decimals(digits) + " " + prefixes[prefix_idx];
  2763. }
  2764. bool String::is_abs_path() const {
  2765. if (length() > 1) {
  2766. return (operator[](0) == '/' || operator[](0) == '\\' || find(":/") != -1 || find(":\\") != -1);
  2767. } else if ((length()) == 1) {
  2768. return (operator[](0) == '/' || operator[](0) == '\\');
  2769. } else {
  2770. return false;
  2771. }
  2772. }
  2773. bool String::is_valid_identifier() const {
  2774. int len = length();
  2775. if (len == 0) {
  2776. return false;
  2777. }
  2778. const wchar_t *str = &operator[](0);
  2779. for (int i = 0; i < len; i++) {
  2780. if (i == 0) {
  2781. if (str[0] >= '0' && str[0] <= '9') {
  2782. return false; // no start with number plz
  2783. }
  2784. }
  2785. bool valid_char = (str[i] >= '0' && str[i] <= '9') || (str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z') || str[i] == '_';
  2786. if (!valid_char) {
  2787. return false;
  2788. }
  2789. }
  2790. return true;
  2791. }
  2792. //kind of poor should be rewritten properly
  2793. String String::word_wrap(int p_chars_per_line) const {
  2794. int from = 0;
  2795. int last_space = 0;
  2796. String ret;
  2797. for (int i = 0; i < length(); i++) {
  2798. if (i - from >= p_chars_per_line) {
  2799. if (last_space == -1) {
  2800. ret += substr(from, i - from + 1) + "\n";
  2801. } else {
  2802. ret += substr(from, last_space - from) + "\n";
  2803. i = last_space; //rewind
  2804. }
  2805. from = i + 1;
  2806. last_space = -1;
  2807. } else if (operator[](i) == ' ' || operator[](i) == '\t') {
  2808. last_space = i;
  2809. } else if (operator[](i) == '\n') {
  2810. ret += substr(from, i - from) + "\n";
  2811. from = i + 1;
  2812. last_space = -1;
  2813. }
  2814. }
  2815. if (from < length()) {
  2816. ret += substr(from, length());
  2817. }
  2818. return ret;
  2819. }
  2820. String String::http_escape() const {
  2821. const CharString temp = utf8();
  2822. String res;
  2823. for (int i = 0; i < temp.length(); ++i) {
  2824. uint8_t ord = temp[i];
  2825. if (ord == '.' || ord == '-' || ord == '_' || ord == '~' ||
  2826. (ord >= 'a' && ord <= 'z') ||
  2827. (ord >= 'A' && ord <= 'Z') ||
  2828. (ord >= '0' && ord <= '9')) {
  2829. res += ord;
  2830. } else {
  2831. char p[4] = { '%', 0, 0, 0 };
  2832. static const char hex[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
  2833. p[1] = hex[ord >> 4];
  2834. p[2] = hex[ord & 0xF];
  2835. res += p;
  2836. }
  2837. }
  2838. return res;
  2839. }
  2840. String String::http_unescape() const {
  2841. String res;
  2842. for (int i = 0; i < length(); ++i) {
  2843. if (ord_at(i) == '%' && i + 2 < length()) {
  2844. CharType ord1 = ord_at(i + 1);
  2845. if ((ord1 >= '0' && ord1 <= '9') || (ord1 >= 'A' && ord1 <= 'Z')) {
  2846. CharType ord2 = ord_at(i + 2);
  2847. if ((ord2 >= '0' && ord2 <= '9') || (ord2 >= 'A' && ord2 <= 'Z')) {
  2848. char bytes[3] = { (char)ord1, (char)ord2, 0 };
  2849. res += (char)strtol(bytes, nullptr, 16);
  2850. i += 2;
  2851. }
  2852. } else {
  2853. res += ord_at(i);
  2854. }
  2855. } else {
  2856. res += ord_at(i);
  2857. }
  2858. }
  2859. return String::utf8(res.ascii());
  2860. }
  2861. String String::c_unescape() const {
  2862. String escaped = *this;
  2863. escaped = escaped.replace("\\a", "\a");
  2864. escaped = escaped.replace("\\b", "\b");
  2865. escaped = escaped.replace("\\f", "\f");
  2866. escaped = escaped.replace("\\n", "\n");
  2867. escaped = escaped.replace("\\r", "\r");
  2868. escaped = escaped.replace("\\t", "\t");
  2869. escaped = escaped.replace("\\v", "\v");
  2870. escaped = escaped.replace("\\'", "\'");
  2871. escaped = escaped.replace("\\\"", "\"");
  2872. escaped = escaped.replace("\\?", "\?");
  2873. escaped = escaped.replace("\\\\", "\\");
  2874. return escaped;
  2875. }
  2876. String String::c_escape() const {
  2877. String escaped = *this;
  2878. escaped = escaped.replace("\\", "\\\\");
  2879. escaped = escaped.replace("\a", "\\a");
  2880. escaped = escaped.replace("\b", "\\b");
  2881. escaped = escaped.replace("\f", "\\f");
  2882. escaped = escaped.replace("\n", "\\n");
  2883. escaped = escaped.replace("\r", "\\r");
  2884. escaped = escaped.replace("\t", "\\t");
  2885. escaped = escaped.replace("\v", "\\v");
  2886. escaped = escaped.replace("\'", "\\'");
  2887. escaped = escaped.replace("\?", "\\?");
  2888. escaped = escaped.replace("\"", "\\\"");
  2889. return escaped;
  2890. }
  2891. String String::c_escape_multiline() const {
  2892. String escaped = *this;
  2893. escaped = escaped.replace("\\", "\\\\");
  2894. escaped = escaped.replace("\"", "\\\"");
  2895. return escaped;
  2896. }
  2897. String String::json_escape() const {
  2898. String escaped = *this;
  2899. escaped = escaped.replace("\\", "\\\\");
  2900. escaped = escaped.replace("\b", "\\b");
  2901. escaped = escaped.replace("\f", "\\f");
  2902. escaped = escaped.replace("\n", "\\n");
  2903. escaped = escaped.replace("\r", "\\r");
  2904. escaped = escaped.replace("\t", "\\t");
  2905. escaped = escaped.replace("\v", "\\v");
  2906. escaped = escaped.replace("\"", "\\\"");
  2907. return escaped;
  2908. }
  2909. String String::xml_escape(bool p_escape_quotes) const {
  2910. String str = *this;
  2911. str = str.replace("&", "&amp;");
  2912. str = str.replace("<", "&lt;");
  2913. str = str.replace(">", "&gt;");
  2914. if (p_escape_quotes) {
  2915. str = str.replace("'", "&apos;");
  2916. str = str.replace("\"", "&quot;");
  2917. }
  2918. /*
  2919. for (int i=1;i<32;i++) {
  2920. char chr[2]={i,0};
  2921. str=str.replace(chr,"&#"+String::num(i)+";");
  2922. }*/
  2923. return str;
  2924. }
  2925. static _FORCE_INLINE_ int _xml_unescape(const CharType *p_src, int p_src_len, CharType *p_dst) {
  2926. int len = 0;
  2927. while (p_src_len) {
  2928. if (*p_src == '&') {
  2929. int eat = 0;
  2930. if (p_src_len >= 4 && p_src[1] == '#') {
  2931. CharType c = 0;
  2932. bool overflow = false;
  2933. if (p_src[2] == 'x') {
  2934. // Hex entity &#x<num>;
  2935. for (int i = 3; i < p_src_len; i++) {
  2936. eat = i + 1;
  2937. CharType ct = p_src[i];
  2938. if (ct == ';') {
  2939. break;
  2940. } else if (ct >= '0' && ct <= '9') {
  2941. ct = ct - '0';
  2942. } else if (ct >= 'a' && ct <= 'f') {
  2943. ct = (ct - 'a') + 10;
  2944. } else if (ct >= 'A' && ct <= 'F') {
  2945. ct = (ct - 'A') + 10;
  2946. } else {
  2947. break;
  2948. }
  2949. if (c > (WCHAR_MAX >> 4)) {
  2950. overflow = true;
  2951. break;
  2952. }
  2953. c <<= 4;
  2954. c |= ct;
  2955. }
  2956. } else {
  2957. // Decimal entity &#<num>;
  2958. for (int i = 2; i < p_src_len; i++) {
  2959. eat = i + 1;
  2960. CharType ct = p_src[i];
  2961. if (ct == ';' || ct < '0' || ct > '9') {
  2962. break;
  2963. }
  2964. }
  2965. if (p_src[eat - 1] == ';') {
  2966. int64_t val = String::to_int(p_src + 2, eat - 3);
  2967. if (val > 0 && val <= WCHAR_MAX) {
  2968. c = (CharType)val;
  2969. } else {
  2970. overflow = true;
  2971. }
  2972. }
  2973. }
  2974. // Value must be non-zero, in the range of char32_t,
  2975. // actually end with ';'. If invalid, leave the entity as-is
  2976. if (c == '\0' || overflow || p_src[eat - 1] != ';') {
  2977. eat = 1;
  2978. c = *p_src;
  2979. }
  2980. if (p_dst) {
  2981. *p_dst = c;
  2982. }
  2983. } else if (p_src_len >= 4 && p_src[1] == 'g' && p_src[2] == 't' && p_src[3] == ';') {
  2984. if (p_dst) {
  2985. *p_dst = '>';
  2986. }
  2987. eat = 4;
  2988. } else if (p_src_len >= 4 && p_src[1] == 'l' && p_src[2] == 't' && p_src[3] == ';') {
  2989. if (p_dst) {
  2990. *p_dst = '<';
  2991. }
  2992. eat = 4;
  2993. } else if (p_src_len >= 5 && p_src[1] == 'a' && p_src[2] == 'm' && p_src[3] == 'p' && p_src[4] == ';') {
  2994. if (p_dst) {
  2995. *p_dst = '&';
  2996. }
  2997. eat = 5;
  2998. } else if (p_src_len >= 6 && p_src[1] == 'q' && p_src[2] == 'u' && p_src[3] == 'o' && p_src[4] == 't' && p_src[5] == ';') {
  2999. if (p_dst) {
  3000. *p_dst = '"';
  3001. }
  3002. eat = 6;
  3003. } else if (p_src_len >= 6 && p_src[1] == 'a' && p_src[2] == 'p' && p_src[3] == 'o' && p_src[4] == 's' && p_src[5] == ';') {
  3004. if (p_dst) {
  3005. *p_dst = '\'';
  3006. }
  3007. eat = 6;
  3008. } else {
  3009. if (p_dst) {
  3010. *p_dst = *p_src;
  3011. }
  3012. eat = 1;
  3013. }
  3014. if (p_dst) {
  3015. p_dst++;
  3016. }
  3017. len++;
  3018. p_src += eat;
  3019. p_src_len -= eat;
  3020. } else {
  3021. if (p_dst) {
  3022. *p_dst = *p_src;
  3023. p_dst++;
  3024. }
  3025. len++;
  3026. p_src++;
  3027. p_src_len--;
  3028. }
  3029. }
  3030. return len;
  3031. }
  3032. String String::xml_unescape() const {
  3033. String str;
  3034. int l = length();
  3035. int len = _xml_unescape(c_str(), l, nullptr);
  3036. if (len == 0) {
  3037. return String();
  3038. }
  3039. str.resize(len + 1);
  3040. _xml_unescape(c_str(), l, str.ptrw());
  3041. str[len] = 0;
  3042. return str;
  3043. }
  3044. String String::pad_decimals(int p_digits) const {
  3045. String s = *this;
  3046. int c = s.find(".");
  3047. if (c == -1) {
  3048. if (p_digits <= 0) {
  3049. return s;
  3050. }
  3051. s += ".";
  3052. c = s.length() - 1;
  3053. } else {
  3054. if (p_digits <= 0) {
  3055. return s.substr(0, c);
  3056. }
  3057. }
  3058. if (s.length() - (c + 1) > p_digits) {
  3059. s = s.substr(0, c + p_digits + 1);
  3060. } else {
  3061. while (s.length() - (c + 1) < p_digits) {
  3062. s += "0";
  3063. }
  3064. }
  3065. return s;
  3066. }
  3067. String String::pad_zeros(int p_digits) const {
  3068. String s = *this;
  3069. int end = s.find(".");
  3070. if (end == -1) {
  3071. end = s.length();
  3072. }
  3073. if (end == 0) {
  3074. return s;
  3075. }
  3076. int begin = 0;
  3077. while (begin < end && (s[begin] < '0' || s[begin] > '9')) {
  3078. begin++;
  3079. }
  3080. if (begin >= end) {
  3081. return s;
  3082. }
  3083. while (end - begin < p_digits) {
  3084. s = s.insert(begin, "0");
  3085. end++;
  3086. }
  3087. return s;
  3088. }
  3089. String String::trim_prefix(const String &p_prefix) const {
  3090. String s = *this;
  3091. if (s.begins_with(p_prefix)) {
  3092. return s.substr(p_prefix.length(), s.length() - p_prefix.length());
  3093. }
  3094. return s;
  3095. }
  3096. String String::trim_suffix(const String &p_suffix) const {
  3097. String s = *this;
  3098. if (s.ends_with(p_suffix)) {
  3099. return s.substr(0, s.length() - p_suffix.length());
  3100. }
  3101. return s;
  3102. }
  3103. bool String::is_valid_integer() const {
  3104. int len = length();
  3105. if (len == 0) {
  3106. return false;
  3107. }
  3108. int from = 0;
  3109. if (len != 1 && (operator[](0) == '+' || operator[](0) == '-')) {
  3110. from++;
  3111. }
  3112. for (int i = from; i < len; i++) {
  3113. if (operator[](i) < '0' || operator[](i) > '9') {
  3114. return false; // no start with number plz
  3115. }
  3116. }
  3117. return true;
  3118. }
  3119. bool String::is_valid_hex_number(bool p_with_prefix) const {
  3120. int len = length();
  3121. if (len == 0) {
  3122. return false;
  3123. }
  3124. int from = 0;
  3125. if (len != 1 && (operator[](0) == '+' || operator[](0) == '-')) {
  3126. from++;
  3127. }
  3128. if (p_with_prefix) {
  3129. if (len < 3) {
  3130. return false;
  3131. }
  3132. if (operator[](from) != '0' || operator[](from + 1) != 'x') {
  3133. return false;
  3134. }
  3135. from += 2;
  3136. }
  3137. for (int i = from; i < len; i++) {
  3138. CharType c = operator[](i);
  3139. if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) {
  3140. continue;
  3141. }
  3142. return false;
  3143. }
  3144. return true;
  3145. };
  3146. bool String::is_valid_float() const {
  3147. int len = length();
  3148. if (len == 0) {
  3149. return false;
  3150. }
  3151. int from = 0;
  3152. if (operator[](0) == '+' || operator[](0) == '-') {
  3153. from++;
  3154. }
  3155. bool exponent_found = false;
  3156. bool period_found = false;
  3157. bool sign_found = false;
  3158. bool exponent_values_found = false;
  3159. bool numbers_found = false;
  3160. for (int i = from; i < len; i++) {
  3161. if (operator[](i) >= '0' && operator[](i) <= '9') {
  3162. if (exponent_found) {
  3163. exponent_values_found = true;
  3164. } else {
  3165. numbers_found = true;
  3166. }
  3167. } else if (numbers_found && !exponent_found && operator[](i) == 'e') {
  3168. exponent_found = true;
  3169. } else if (!period_found && !exponent_found && operator[](i) == '.') {
  3170. period_found = true;
  3171. } else if ((operator[](i) == '-' || operator[](i) == '+') && exponent_found && !exponent_values_found && !sign_found) {
  3172. sign_found = true;
  3173. } else {
  3174. return false; // no start with number plz
  3175. }
  3176. }
  3177. return numbers_found;
  3178. }
  3179. String String::path_to_file(const String &p_path) const {
  3180. // Don't get base dir for src, this is expected to be a dir already.
  3181. String src = this->replace("\\", "/");
  3182. String dst = p_path.replace("\\", "/").get_base_dir();
  3183. String rel = src.path_to(dst);
  3184. if (rel == dst) { // failed
  3185. return p_path;
  3186. } else {
  3187. return rel + p_path.get_file();
  3188. }
  3189. }
  3190. String String::path_to(const String &p_path) const {
  3191. String src = this->replace("\\", "/");
  3192. String dst = p_path.replace("\\", "/");
  3193. if (!src.ends_with("/")) {
  3194. src += "/";
  3195. }
  3196. if (!dst.ends_with("/")) {
  3197. dst += "/";
  3198. }
  3199. String base;
  3200. if (src.begins_with("res://") && dst.begins_with("res://")) {
  3201. base = "res:/";
  3202. src = src.replace("res://", "/");
  3203. dst = dst.replace("res://", "/");
  3204. } else if (src.begins_with("user://") && dst.begins_with("user://")) {
  3205. base = "user:/";
  3206. src = src.replace("user://", "/");
  3207. dst = dst.replace("user://", "/");
  3208. } else if (src.begins_with("/") && dst.begins_with("/")) {
  3209. //nothing
  3210. } else {
  3211. //dos style
  3212. String src_begin = src.get_slicec('/', 0);
  3213. String dst_begin = dst.get_slicec('/', 0);
  3214. if (src_begin != dst_begin) {
  3215. return p_path; //impossible to do this
  3216. }
  3217. base = src_begin;
  3218. src = src.substr(src_begin.length(), src.length());
  3219. dst = dst.substr(dst_begin.length(), dst.length());
  3220. }
  3221. //remove leading and trailing slash and split
  3222. Vector<String> src_dirs = src.substr(1, src.length() - 2).split("/");
  3223. Vector<String> dst_dirs = dst.substr(1, dst.length() - 2).split("/");
  3224. //find common parent
  3225. int common_parent = 0;
  3226. while (true) {
  3227. if (src_dirs.size() == common_parent) {
  3228. break;
  3229. }
  3230. if (dst_dirs.size() == common_parent) {
  3231. break;
  3232. }
  3233. if (src_dirs[common_parent] != dst_dirs[common_parent]) {
  3234. break;
  3235. }
  3236. common_parent++;
  3237. }
  3238. common_parent--;
  3239. String dir;
  3240. for (int i = src_dirs.size() - 1; i > common_parent; i--) {
  3241. dir += "../";
  3242. }
  3243. for (int i = common_parent + 1; i < dst_dirs.size(); i++) {
  3244. dir += dst_dirs[i] + "/";
  3245. }
  3246. if (dir.length() == 0) {
  3247. dir = "./";
  3248. }
  3249. return dir;
  3250. }
  3251. bool String::is_valid_html_color() const {
  3252. return Color::html_is_valid(*this);
  3253. }
  3254. bool String::is_valid_filename() const {
  3255. String stripped = strip_edges();
  3256. if (*this != stripped) {
  3257. return false;
  3258. }
  3259. if (stripped == String()) {
  3260. return false;
  3261. }
  3262. return !(find(":") != -1 || find("/") != -1 || find("\\") != -1 || find("?") != -1 || find("*") != -1 || find("\"") != -1 || find("|") != -1 || find("%") != -1 || find("<") != -1 || find(">") != -1);
  3263. }
  3264. bool String::is_valid_ip_address() const {
  3265. if (find(":") >= 0) {
  3266. Vector<String> ip = split(":");
  3267. for (int i = 0; i < ip.size(); i++) {
  3268. String n = ip[i];
  3269. if (n.empty()) {
  3270. continue;
  3271. }
  3272. if (n.is_valid_hex_number(false)) {
  3273. int nint = n.hex_to_int(false);
  3274. if (nint < 0 || nint > 0xffff) {
  3275. return false;
  3276. }
  3277. continue;
  3278. };
  3279. if (!n.is_valid_ip_address()) {
  3280. return false;
  3281. }
  3282. };
  3283. } else {
  3284. Vector<String> ip = split(".");
  3285. if (ip.size() != 4) {
  3286. return false;
  3287. }
  3288. for (int i = 0; i < ip.size(); i++) {
  3289. String n = ip[i];
  3290. if (!n.is_valid_integer()) {
  3291. return false;
  3292. }
  3293. int val = n.to_int();
  3294. if (val < 0 || val > 255) {
  3295. return false;
  3296. }
  3297. }
  3298. };
  3299. return true;
  3300. }
  3301. bool String::is_resource_file() const {
  3302. return begins_with("res://") && find("::") == -1;
  3303. }
  3304. bool String::is_rel_path() const {
  3305. return !is_abs_path();
  3306. }
  3307. String String::get_base_dir() const {
  3308. int end = 0;
  3309. // url scheme style base
  3310. int basepos = find("://");
  3311. if (basepos != -1) {
  3312. end = basepos + 3;
  3313. }
  3314. // windows top level directory base
  3315. if (end == 0) {
  3316. basepos = find(":/");
  3317. if (basepos == -1) {
  3318. basepos = find(":\\");
  3319. }
  3320. if (basepos != -1) {
  3321. end = basepos + 2;
  3322. }
  3323. }
  3324. // unix root directory base
  3325. if (end == 0) {
  3326. if (begins_with("/")) {
  3327. end = 1;
  3328. }
  3329. }
  3330. String rs;
  3331. String base;
  3332. if (end != 0) {
  3333. rs = substr(end, length());
  3334. base = substr(0, end);
  3335. } else {
  3336. rs = *this;
  3337. }
  3338. int sep = MAX(rs.rfind("/"), rs.rfind("\\"));
  3339. if (sep == -1) {
  3340. return base;
  3341. }
  3342. return base + rs.substr(0, sep);
  3343. }
  3344. String String::get_file() const {
  3345. int sep = MAX(find_last("/"), find_last("\\"));
  3346. if (sep == -1) {
  3347. return *this;
  3348. }
  3349. return substr(sep + 1, length());
  3350. }
  3351. String String::get_extension() const {
  3352. int pos = find_last(".");
  3353. if (pos < 0 || pos < MAX(find_last("/"), find_last("\\"))) {
  3354. return "";
  3355. }
  3356. return substr(pos + 1, length());
  3357. }
  3358. String String::plus_file(const String &p_file) const {
  3359. if (empty()) {
  3360. return p_file;
  3361. }
  3362. if (operator[](length() - 1) == '/' || (p_file.size() > 0 && p_file.operator[](0) == '/')) {
  3363. return *this + p_file;
  3364. }
  3365. return *this + "/" + p_file;
  3366. }
  3367. String String::percent_encode() const {
  3368. CharString cs = utf8();
  3369. String encoded;
  3370. for (int i = 0; i < cs.length(); i++) {
  3371. uint8_t c = cs[i];
  3372. if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '-' || c == '_' || c == '~' || c == '.') {
  3373. char p[2] = { (char)c, 0 };
  3374. encoded += p;
  3375. } else {
  3376. char p[4] = { '%', 0, 0, 0 };
  3377. static const char hex[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
  3378. p[1] = hex[c >> 4];
  3379. p[2] = hex[c & 0xF];
  3380. encoded += p;
  3381. }
  3382. }
  3383. return encoded;
  3384. }
  3385. String String::percent_decode() const {
  3386. CharString pe;
  3387. CharString cs = utf8();
  3388. for (int i = 0; i < cs.length(); i++) {
  3389. uint8_t c = cs[i];
  3390. if (c == '%' && i < length() - 2) {
  3391. uint8_t a = LOWERCASE(cs[i + 1]);
  3392. uint8_t b = LOWERCASE(cs[i + 2]);
  3393. if (a >= '0' && a <= '9') {
  3394. c = (a - '0') << 4;
  3395. } else if (a >= 'a' && a <= 'f') {
  3396. c = (a - 'a' + 10) << 4;
  3397. } else {
  3398. continue;
  3399. }
  3400. uint8_t d = 0;
  3401. if (b >= '0' && b <= '9') {
  3402. d = (b - '0');
  3403. } else if (b >= 'a' && b <= 'f') {
  3404. d = (b - 'a' + 10);
  3405. } else {
  3406. continue;
  3407. }
  3408. c += d;
  3409. i += 2;
  3410. }
  3411. pe += c;
  3412. }
  3413. return String::utf8(pe.ptr());
  3414. }
  3415. String String::property_name_encode() const {
  3416. // Escape and quote strings with extended ASCII or further Unicode characters
  3417. // as well as '"', '=' or ' ' (32)
  3418. const CharType *cstr = c_str();
  3419. for (int i = 0; cstr[i]; i++) {
  3420. if (cstr[i] == '=' || cstr[i] == '"' || cstr[i] < 33 || cstr[i] > 126) {
  3421. return "\"" + c_escape_multiline() + "\"";
  3422. }
  3423. }
  3424. // Keep as is
  3425. return *this;
  3426. }
  3427. // Changes made to the set of invalid characters must also be reflected in the String documentation.
  3428. const String String::invalid_node_name_characters = ". : @ / \"";
  3429. String String::validate_node_name() const {
  3430. Vector<String> chars = String::invalid_node_name_characters.split(" ");
  3431. String name = this->replace(chars[0], "");
  3432. for (int i = 1; i < chars.size(); i++) {
  3433. name = name.replace(chars[i], "");
  3434. }
  3435. return name;
  3436. }
  3437. String String::get_basename() const {
  3438. int pos = find_last(".");
  3439. if (pos < 0 || pos < MAX(find_last("/"), find_last("\\"))) {
  3440. return *this;
  3441. }
  3442. return substr(0, pos);
  3443. }
  3444. String itos(int64_t p_val) {
  3445. return String::num_int64(p_val);
  3446. }
  3447. String uitos(uint64_t p_val) {
  3448. return String::num_uint64(p_val);
  3449. }
  3450. String rtos(double p_val) {
  3451. return String::num(p_val);
  3452. }
  3453. String rtoss(double p_val) {
  3454. return String::num_scientific(p_val);
  3455. }
  3456. // Right-pad with a character.
  3457. String String::rpad(int min_length, const String &character) const {
  3458. String s = *this;
  3459. int padding = min_length - s.length();
  3460. if (padding > 0) {
  3461. for (int i = 0; i < padding; i++) {
  3462. s = s + character;
  3463. }
  3464. }
  3465. return s;
  3466. }
  3467. // Left-pad with a character.
  3468. String String::lpad(int min_length, const String &character) const {
  3469. String s = *this;
  3470. int padding = min_length - s.length();
  3471. if (padding > 0) {
  3472. for (int i = 0; i < padding; i++) {
  3473. s = character + s;
  3474. }
  3475. }
  3476. return s;
  3477. }
  3478. // sprintf is implemented in GDScript via:
  3479. // "fish %s pie" % "frog"
  3480. // "fish %s %d pie" % ["frog", 12]
  3481. // In case of an error, the string returned is the error description and "error" is true.
  3482. String String::sprintf(const Array &values, bool *error) const {
  3483. String formatted;
  3484. CharType *self = (CharType *)c_str();
  3485. bool in_format = false;
  3486. int value_index = 0;
  3487. int min_chars = 0;
  3488. int min_decimals = 0;
  3489. bool in_decimals = false;
  3490. bool pad_with_zeros = false;
  3491. bool left_justified = false;
  3492. bool show_sign = false;
  3493. *error = true;
  3494. for (; *self; self++) {
  3495. const CharType c = *self;
  3496. if (in_format) { // We have % - lets see what else we get.
  3497. switch (c) {
  3498. case '%': { // Replace %% with %
  3499. formatted += chr(c);
  3500. in_format = false;
  3501. break;
  3502. }
  3503. case 'd': // Integer (signed)
  3504. case 'o': // Octal
  3505. case 'x': // Hexadecimal (lowercase)
  3506. case 'X': { // Hexadecimal (uppercase)
  3507. if (value_index >= values.size()) {
  3508. return "not enough arguments for format string";
  3509. }
  3510. if (!values[value_index].is_num()) {
  3511. return "a number is required";
  3512. }
  3513. int64_t value = values[value_index];
  3514. int base = 16;
  3515. bool capitalize = false;
  3516. switch (c) {
  3517. case 'd':
  3518. base = 10;
  3519. break;
  3520. case 'o':
  3521. base = 8;
  3522. break;
  3523. case 'x':
  3524. break;
  3525. case 'X':
  3526. base = 16;
  3527. capitalize = true;
  3528. break;
  3529. }
  3530. // Get basic number.
  3531. String str = String::num_int64(ABS(value), base, capitalize);
  3532. int number_len = str.length();
  3533. // Padding.
  3534. int pad_chars_count = (value < 0 || show_sign) ? min_chars - 1 : min_chars;
  3535. String pad_char = pad_with_zeros ? String("0") : String(" ");
  3536. if (left_justified) {
  3537. str = str.rpad(pad_chars_count, pad_char);
  3538. } else {
  3539. str = str.lpad(pad_chars_count, pad_char);
  3540. }
  3541. // Sign.
  3542. if (show_sign || value < 0) {
  3543. String sign_char = value < 0 ? "-" : "+";
  3544. if (left_justified) {
  3545. str = str.insert(0, sign_char);
  3546. } else {
  3547. str = str.insert(pad_with_zeros ? 0 : str.length() - number_len, sign_char);
  3548. }
  3549. }
  3550. formatted += str;
  3551. ++value_index;
  3552. in_format = false;
  3553. break;
  3554. }
  3555. case 'f': { // Float
  3556. if (value_index >= values.size()) {
  3557. return "not enough arguments for format string";
  3558. }
  3559. if (!values[value_index].is_num()) {
  3560. return "a number is required";
  3561. }
  3562. double value = values[value_index];
  3563. bool is_negative = (value < 0);
  3564. String str = String::num(ABS(value), min_decimals);
  3565. // Pad decimals out.
  3566. str = str.pad_decimals(min_decimals);
  3567. int initial_len = str.length();
  3568. // Padding. Leave room for sign later if required.
  3569. int pad_chars_count = (is_negative || show_sign) ? min_chars - 1 : min_chars;
  3570. String pad_char = pad_with_zeros ? String("0") : String(" ");
  3571. if (left_justified) {
  3572. str = str.rpad(pad_chars_count, pad_char);
  3573. } else {
  3574. str = str.lpad(pad_chars_count, pad_char);
  3575. }
  3576. // Add sign if needed.
  3577. if (show_sign || is_negative) {
  3578. String sign_char = is_negative ? "-" : "+";
  3579. if (left_justified) {
  3580. str = str.insert(0, sign_char);
  3581. } else {
  3582. str = str.insert(pad_with_zeros ? 0 : str.length() - initial_len, sign_char);
  3583. }
  3584. }
  3585. formatted += str;
  3586. ++value_index;
  3587. in_format = false;
  3588. break;
  3589. }
  3590. case 's': { // String
  3591. if (value_index >= values.size()) {
  3592. return "not enough arguments for format string";
  3593. }
  3594. String str = values[value_index];
  3595. // Padding.
  3596. if (left_justified) {
  3597. str = str.rpad(min_chars);
  3598. } else {
  3599. str = str.lpad(min_chars);
  3600. }
  3601. formatted += str;
  3602. ++value_index;
  3603. in_format = false;
  3604. break;
  3605. }
  3606. case 'c': {
  3607. if (value_index >= values.size()) {
  3608. return "not enough arguments for format string";
  3609. }
  3610. // Convert to character.
  3611. String str;
  3612. if (values[value_index].is_num()) {
  3613. int value = values[value_index];
  3614. if (value < 0) {
  3615. return "unsigned byte integer is lower than maximum";
  3616. } else if (value > 255) {
  3617. return "unsigned byte integer is greater than maximum";
  3618. }
  3619. str = chr(values[value_index]);
  3620. } else if (values[value_index].get_type() == Variant::STRING) {
  3621. str = values[value_index];
  3622. if (str.length() != 1) {
  3623. return "%c requires number or single-character string";
  3624. }
  3625. } else {
  3626. return "%c requires number or single-character string";
  3627. }
  3628. // Padding.
  3629. if (left_justified) {
  3630. str = str.rpad(min_chars);
  3631. } else {
  3632. str = str.lpad(min_chars);
  3633. }
  3634. formatted += str;
  3635. ++value_index;
  3636. in_format = false;
  3637. break;
  3638. }
  3639. case '-': { // Left justify
  3640. left_justified = true;
  3641. break;
  3642. }
  3643. case '+': { // Show + if positive.
  3644. show_sign = true;
  3645. break;
  3646. }
  3647. case '0':
  3648. case '1':
  3649. case '2':
  3650. case '3':
  3651. case '4':
  3652. case '5':
  3653. case '6':
  3654. case '7':
  3655. case '8':
  3656. case '9': {
  3657. int n = c - '0';
  3658. if (in_decimals) {
  3659. min_decimals *= 10;
  3660. min_decimals += n;
  3661. } else {
  3662. if (c == '0' && min_chars == 0) {
  3663. if (left_justified) {
  3664. WARN_PRINT("'0' flag ignored with '-' flag in string format");
  3665. } else {
  3666. pad_with_zeros = true;
  3667. }
  3668. } else {
  3669. min_chars *= 10;
  3670. min_chars += n;
  3671. }
  3672. }
  3673. break;
  3674. }
  3675. case '.': { // Float separator.
  3676. if (in_decimals) {
  3677. return "too many decimal points in format";
  3678. }
  3679. in_decimals = true;
  3680. min_decimals = 0; // We want to add the value manually.
  3681. break;
  3682. }
  3683. case '*': { // Dynamic width, based on value.
  3684. if (value_index >= values.size()) {
  3685. return "not enough arguments for format string";
  3686. }
  3687. if (!values[value_index].is_num()) {
  3688. return "* wants number";
  3689. }
  3690. int size = values[value_index];
  3691. if (in_decimals) {
  3692. min_decimals = size;
  3693. } else {
  3694. min_chars = size;
  3695. }
  3696. ++value_index;
  3697. break;
  3698. }
  3699. default: {
  3700. return "unsupported format character";
  3701. }
  3702. }
  3703. } else { // Not in format string.
  3704. switch (c) {
  3705. case '%':
  3706. in_format = true;
  3707. // Back to defaults:
  3708. min_chars = 0;
  3709. min_decimals = 6;
  3710. pad_with_zeros = false;
  3711. left_justified = false;
  3712. show_sign = false;
  3713. in_decimals = false;
  3714. break;
  3715. default:
  3716. formatted += chr(c);
  3717. }
  3718. }
  3719. }
  3720. if (in_format) {
  3721. return "incomplete format";
  3722. }
  3723. if (value_index != values.size()) {
  3724. return "not all arguments converted during string formatting";
  3725. }
  3726. *error = false;
  3727. return formatted;
  3728. }
  3729. String String::quote(String quotechar) const {
  3730. return quotechar + *this + quotechar;
  3731. }
  3732. String String::unquote() const {
  3733. if (!is_quoted()) {
  3734. return *this;
  3735. }
  3736. return substr(1, length() - 2);
  3737. }
  3738. #ifdef TOOLS_ENABLED
  3739. String TTR(const String &p_text) {
  3740. if (TranslationServer::get_singleton()) {
  3741. return TranslationServer::get_singleton()->tool_translate(p_text);
  3742. }
  3743. return p_text;
  3744. }
  3745. String DTR(const String &p_text) {
  3746. // Comes straight from the XML, so remove indentation and any trailing whitespace.
  3747. const String text = p_text.dedent().strip_edges();
  3748. if (TranslationServer::get_singleton()) {
  3749. return TranslationServer::get_singleton()->doc_translate(text);
  3750. }
  3751. return text;
  3752. }
  3753. #endif
  3754. String RTR(const String &p_text) {
  3755. if (TranslationServer::get_singleton()) {
  3756. String rtr = TranslationServer::get_singleton()->tool_translate(p_text);
  3757. if (rtr == String() || rtr == p_text) {
  3758. return TranslationServer::get_singleton()->translate(p_text);
  3759. } else {
  3760. return rtr;
  3761. }
  3762. }
  3763. return p_text;
  3764. }