cmList.cxx 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmConfigure.h" // IWYU pragma: keep
  4. #include "cmList.h"
  5. #include <algorithm>
  6. #include <cstddef>
  7. #include <functional>
  8. #include <iterator>
  9. #include <set>
  10. #include <stdexcept>
  11. #include <utility>
  12. #include <cm/memory>
  13. #include "cmsys/RegularExpression.hxx"
  14. #include "cmAlgorithms.h"
  15. #include "cmGeneratorExpression.h"
  16. #include "cmListFileCache.h"
  17. #include "cmRange.h"
  18. #include "cmStringAlgorithms.h"
  19. #include "cmStringReplaceHelper.h"
  20. #include "cmSystemTools.h"
  21. cm::string_view cmList::element_separator{ ";" };
  22. cmList cmList::sublist(size_type pos, size_type length) const
  23. {
  24. if (pos >= this->Values.size()) {
  25. throw std::out_of_range(cmStrCat(
  26. "begin index: ", pos, " is out of range 0 - ", this->Values.size() - 1));
  27. }
  28. size_type count = (length == npos || pos + length > this->size())
  29. ? this->size()
  30. : pos + length;
  31. return this->sublist(this->begin() + pos, this->begin() + count);
  32. }
  33. cmList::size_type cmList::find(cm::string_view value) const
  34. {
  35. auto res = std::find(this->Values.begin(), this->Values.end(), value);
  36. if (res == this->Values.end()) {
  37. return npos;
  38. }
  39. return std::distance(this->Values.begin(), res);
  40. }
  41. cmList& cmList::remove_duplicates()
  42. {
  43. auto newEnd = cmRemoveDuplicates(this->Values);
  44. this->Values.erase(newEnd, this->Values.end());
  45. return *this;
  46. }
  47. namespace {
  48. class MatchesRegex
  49. {
  50. public:
  51. MatchesRegex(cmsys::RegularExpression& regex, cmList::FilterMode mode)
  52. : Regex(regex)
  53. , IncludeMatches(mode == cmList::FilterMode::INCLUDE)
  54. {
  55. }
  56. bool operator()(const std::string& target)
  57. {
  58. return this->Regex.find(target) ^ this->IncludeMatches;
  59. }
  60. private:
  61. cmsys::RegularExpression& Regex;
  62. const bool IncludeMatches;
  63. };
  64. }
  65. cmList& cmList::filter(cm::string_view pattern, FilterMode mode)
  66. {
  67. cmsys::RegularExpression regex(std::string{ pattern });
  68. if (!regex.is_valid()) {
  69. throw std::invalid_argument(
  70. cmStrCat("sub-command FILTER, mode REGEX failed to compile regex \"",
  71. pattern, "\"."));
  72. }
  73. auto it = std::remove_if(this->Values.begin(), this->Values.end(),
  74. MatchesRegex{ regex, mode });
  75. this->Values.erase(it, this->Values.end());
  76. return *this;
  77. }
  78. namespace {
  79. class StringSorter
  80. {
  81. protected:
  82. using StringFilter = std::function<std::string(const std::string&)>;
  83. using OrderMode = cmList::SortConfiguration::OrderMode;
  84. using CompareMethod = cmList::SortConfiguration::CompareMethod;
  85. using CaseSensitivity = cmList::SortConfiguration::CaseSensitivity;
  86. StringFilter GetCompareFilter(CompareMethod compare)
  87. {
  88. return (compare == CompareMethod::FILE_BASENAME)
  89. ? cmSystemTools::GetFilenameName
  90. : nullptr;
  91. }
  92. StringFilter GetCaseFilter(CaseSensitivity sensitivity)
  93. {
  94. return (sensitivity == CaseSensitivity::INSENSITIVE)
  95. ? cmSystemTools::LowerCase
  96. : nullptr;
  97. }
  98. using ComparisonFunction =
  99. std::function<bool(const std::string&, const std::string&)>;
  100. ComparisonFunction GetComparisonFunction(CompareMethod compare)
  101. {
  102. if (compare == CompareMethod::NATURAL) {
  103. return std::function<bool(const std::string&, const std::string&)>(
  104. [](const std::string& x, const std::string& y) {
  105. return cmSystemTools::strverscmp(x, y) < 0;
  106. });
  107. }
  108. return std::function<bool(const std::string&, const std::string&)>(
  109. [](const std::string& x, const std::string& y) { return x < y; });
  110. }
  111. public:
  112. StringSorter(cmList::SortConfiguration const& config)
  113. : Filters{ this->GetCompareFilter(config.Compare),
  114. this->GetCaseFilter(config.Case) }
  115. , SortMethod(this->GetComparisonFunction(config.Compare))
  116. , Descending(config.Order == OrderMode::DESCENDING)
  117. {
  118. }
  119. std::string ApplyFilter(const std::string& argument)
  120. {
  121. std::string result = argument;
  122. for (auto const& filter : this->Filters) {
  123. if (filter) {
  124. result = filter(result);
  125. }
  126. }
  127. return result;
  128. }
  129. bool operator()(const std::string& a, const std::string& b)
  130. {
  131. std::string af = this->ApplyFilter(a);
  132. std::string bf = this->ApplyFilter(b);
  133. bool result;
  134. if (this->Descending) {
  135. result = this->SortMethod(bf, af);
  136. } else {
  137. result = this->SortMethod(af, bf);
  138. }
  139. return result;
  140. }
  141. private:
  142. StringFilter Filters[2] = { nullptr, nullptr };
  143. ComparisonFunction SortMethod;
  144. bool Descending;
  145. };
  146. }
  147. cmList::SortConfiguration::SortConfiguration() = default;
  148. cmList& cmList::sort(const SortConfiguration& cfg)
  149. {
  150. SortConfiguration config{ cfg };
  151. if (config.Order == SortConfiguration::OrderMode::DEFAULT) {
  152. config.Order = SortConfiguration::OrderMode::ASCENDING;
  153. }
  154. if (config.Compare == SortConfiguration::CompareMethod::DEFAULT) {
  155. config.Compare = SortConfiguration::CompareMethod::STRING;
  156. }
  157. if (config.Case == SortConfiguration::CaseSensitivity::DEFAULT) {
  158. config.Case = SortConfiguration::CaseSensitivity::SENSITIVE;
  159. }
  160. if ((config.Compare == SortConfiguration::CompareMethod::STRING) &&
  161. (config.Case == SortConfiguration::CaseSensitivity::SENSITIVE) &&
  162. (config.Order == SortConfiguration::OrderMode::ASCENDING)) {
  163. std::sort(this->Values.begin(), this->Values.end());
  164. } else {
  165. StringSorter sorter(config);
  166. std::sort(this->Values.begin(), this->Values.end(), sorter);
  167. }
  168. return *this;
  169. }
  170. namespace {
  171. using transform_type = std::function<std::string(const std::string&)>;
  172. using transform_error = cmList::transform_error;
  173. class TransformSelector : public cmList::TransformSelector
  174. {
  175. public:
  176. ~TransformSelector() override = default;
  177. std::string Tag;
  178. const std::string& GetTag() override { return this->Tag; }
  179. virtual bool Validate(std::size_t count = 0) = 0;
  180. virtual bool InSelection(const std::string&) = 0;
  181. virtual void Transform(cmList::container_type& list,
  182. const transform_type& transform)
  183. {
  184. std::transform(list.begin(), list.end(), list.begin(), transform);
  185. }
  186. protected:
  187. TransformSelector(std::string&& tag)
  188. : Tag(std::move(tag))
  189. {
  190. }
  191. };
  192. class TransformNoSelector : public TransformSelector
  193. {
  194. public:
  195. TransformNoSelector()
  196. : TransformSelector("NO SELECTOR")
  197. {
  198. }
  199. bool Validate(std::size_t) override { return true; }
  200. bool InSelection(const std::string&) override { return true; }
  201. };
  202. class TransformSelectorRegex : public TransformSelector
  203. {
  204. public:
  205. TransformSelectorRegex(const std::string& regex)
  206. : TransformSelector("REGEX")
  207. , Regex(regex)
  208. {
  209. }
  210. TransformSelectorRegex(std::string&& regex)
  211. : TransformSelector("REGEX")
  212. , Regex(regex)
  213. {
  214. }
  215. bool Validate(std::size_t) override { return this->Regex.is_valid(); }
  216. bool InSelection(const std::string& value) override
  217. {
  218. return this->Regex.find(value);
  219. }
  220. cmsys::RegularExpression Regex;
  221. };
  222. class TransformSelectorIndexes : public TransformSelector
  223. {
  224. public:
  225. std::vector<index_type> Indexes;
  226. bool InSelection(const std::string&) override { return true; }
  227. void Transform(std::vector<std::string>& list,
  228. const transform_type& transform) override
  229. {
  230. this->Validate(list.size());
  231. for (auto index : this->Indexes) {
  232. list[index] = transform(list[index]);
  233. }
  234. }
  235. protected:
  236. TransformSelectorIndexes(std::string&& tag)
  237. : TransformSelector(std::move(tag))
  238. {
  239. }
  240. TransformSelectorIndexes(std::string&& tag,
  241. std::vector<index_type> const& indexes)
  242. : TransformSelector(std::move(tag))
  243. , Indexes(indexes)
  244. {
  245. }
  246. TransformSelectorIndexes(std::string&& tag,
  247. std::vector<index_type>&& indexes)
  248. : TransformSelector(std::move(tag))
  249. , Indexes(indexes)
  250. {
  251. }
  252. index_type NormalizeIndex(index_type index, std::size_t count)
  253. {
  254. if (index < 0) {
  255. index = static_cast<index_type>(count) + index;
  256. }
  257. if (index < 0 || count <= static_cast<std::size_t>(index)) {
  258. throw transform_error(cmStrCat(
  259. "sub-command TRANSFORM, selector ", this->Tag, ", index: ", index,
  260. " out of range (-", count, ", ", count - 1, ")."));
  261. }
  262. return index;
  263. }
  264. };
  265. class TransformSelectorAt : public TransformSelectorIndexes
  266. {
  267. public:
  268. TransformSelectorAt(std::vector<index_type> const& indexes)
  269. : TransformSelectorIndexes("AT", indexes)
  270. {
  271. }
  272. TransformSelectorAt(std::vector<index_type>&& indexes)
  273. : TransformSelectorIndexes("AT", std::move(indexes))
  274. {
  275. }
  276. bool Validate(std::size_t count) override
  277. {
  278. decltype(this->Indexes) indexes;
  279. for (auto index : this->Indexes) {
  280. indexes.push_back(this->NormalizeIndex(index, count));
  281. }
  282. this->Indexes = std::move(indexes);
  283. return true;
  284. }
  285. };
  286. class TransformSelectorFor : public TransformSelectorIndexes
  287. {
  288. public:
  289. TransformSelectorFor(index_type start, index_type stop, index_type step)
  290. : TransformSelectorIndexes("FOR")
  291. , Start(start)
  292. , Stop(stop)
  293. , Step(step)
  294. {
  295. }
  296. bool Validate(std::size_t count) override
  297. {
  298. this->Start = this->NormalizeIndex(this->Start, count);
  299. this->Stop = this->NormalizeIndex(this->Stop, count);
  300. // Does stepping move us further from the end?
  301. if (this->Start > this->Stop) {
  302. throw transform_error(
  303. cmStrCat("sub-command TRANSFORM, selector FOR "
  304. "expects <start> to be no greater than <stop> (",
  305. this->Start, " > ", this->Stop, ")"));
  306. }
  307. // compute indexes
  308. auto size = (this->Stop - this->Start + 1) / this->Step;
  309. if ((this->Stop - this->Start + 1) % this->Step != 0) {
  310. size += 1;
  311. }
  312. this->Indexes.resize(size);
  313. auto start = this->Start;
  314. auto step = this->Step;
  315. std::generate(this->Indexes.begin(), this->Indexes.end(),
  316. [&start, step]() -> index_type {
  317. auto r = start;
  318. start += step;
  319. return r;
  320. });
  321. return true;
  322. }
  323. private:
  324. index_type Start, Stop, Step;
  325. };
  326. class TransformAction
  327. {
  328. public:
  329. virtual ~TransformAction() = default;
  330. void Initialize(TransformSelector* selector) { this->Selector = selector; }
  331. virtual void Initialize(TransformSelector*, const std::string&) {}
  332. virtual void Initialize(TransformSelector*, const std::string&,
  333. const std::string&)
  334. {
  335. }
  336. virtual void Initialize(TransformSelector* selector,
  337. const std::vector<std::string>&)
  338. {
  339. this->Initialize(selector);
  340. }
  341. virtual std::string operator()(const std::string& s) = 0;
  342. protected:
  343. TransformSelector* Selector;
  344. };
  345. class TransformActionAppend : public TransformAction
  346. {
  347. public:
  348. using TransformAction::Initialize;
  349. void Initialize(TransformSelector* selector,
  350. const std::string& append) override
  351. {
  352. TransformAction::Initialize(selector);
  353. this->Append = append;
  354. }
  355. void Initialize(TransformSelector* selector,
  356. const std::vector<std::string>& append) override
  357. {
  358. this->Initialize(selector, append.front());
  359. }
  360. std::string operator()(const std::string& s) override
  361. {
  362. if (this->Selector->InSelection(s)) {
  363. return cmStrCat(s, this->Append);
  364. }
  365. return s;
  366. }
  367. private:
  368. std::string Append;
  369. };
  370. class TransformActionPrepend : public TransformAction
  371. {
  372. public:
  373. using TransformAction::Initialize;
  374. void Initialize(TransformSelector* selector,
  375. const std::string& prepend) override
  376. {
  377. TransformAction::Initialize(selector);
  378. this->Prepend = prepend;
  379. }
  380. void Initialize(TransformSelector* selector,
  381. const std::vector<std::string>& prepend) override
  382. {
  383. this->Initialize(selector, prepend.front());
  384. }
  385. std::string operator()(const std::string& s) override
  386. {
  387. if (this->Selector->InSelection(s)) {
  388. return cmStrCat(this->Prepend, s);
  389. }
  390. return s;
  391. }
  392. private:
  393. std::string Prepend;
  394. };
  395. class TransformActionToUpper : public TransformAction
  396. {
  397. public:
  398. std::string operator()(const std::string& s) override
  399. {
  400. if (this->Selector->InSelection(s)) {
  401. return cmSystemTools::UpperCase(s);
  402. }
  403. return s;
  404. }
  405. };
  406. class TransformActionToLower : public TransformAction
  407. {
  408. public:
  409. std::string operator()(const std::string& s) override
  410. {
  411. if (this->Selector->InSelection(s)) {
  412. return cmSystemTools::LowerCase(s);
  413. }
  414. return s;
  415. }
  416. };
  417. class TransformActionStrip : public TransformAction
  418. {
  419. public:
  420. std::string operator()(const std::string& s) override
  421. {
  422. if (this->Selector->InSelection(s)) {
  423. return cmTrimWhitespace(s);
  424. }
  425. return s;
  426. }
  427. };
  428. class TransformActionGenexStrip : public TransformAction
  429. {
  430. public:
  431. std::string operator()(const std::string& s) override
  432. {
  433. if (this->Selector->InSelection(s)) {
  434. return cmGeneratorExpression::Preprocess(
  435. s, cmGeneratorExpression::StripAllGeneratorExpressions);
  436. }
  437. return s;
  438. }
  439. };
  440. class TransformActionReplace : public TransformAction
  441. {
  442. public:
  443. using TransformAction::Initialize;
  444. void Initialize(TransformSelector* selector, const std::string& regex,
  445. const std::string& replace) override
  446. {
  447. TransformAction::Initialize(selector);
  448. this->ReplaceHelper =
  449. cm::make_unique<cmStringReplaceHelper>(regex, replace);
  450. if (!this->ReplaceHelper->IsRegularExpressionValid()) {
  451. throw transform_error(
  452. cmStrCat("sub-command TRANSFORM, action REPLACE: Failed to compile "
  453. "regex \"",
  454. regex, "\"."));
  455. }
  456. if (!this->ReplaceHelper->IsReplaceExpressionValid()) {
  457. throw transform_error(cmStrCat("sub-command TRANSFORM, action REPLACE: ",
  458. this->ReplaceHelper->GetError(), "."));
  459. }
  460. }
  461. void Initialize(TransformSelector* selector,
  462. const std::vector<std::string>& args) override
  463. {
  464. this->Initialize(selector, args[0], args[1]);
  465. }
  466. std::string operator()(const std::string& s) override
  467. {
  468. if (this->Selector->InSelection(s)) {
  469. // Scan through the input for all matches.
  470. std::string output;
  471. if (!this->ReplaceHelper->Replace(s, output)) {
  472. throw transform_error(
  473. cmStrCat("sub-command TRANSFORM, action REPLACE: ",
  474. this->ReplaceHelper->GetError(), "."));
  475. }
  476. return output;
  477. }
  478. return s;
  479. }
  480. private:
  481. std::unique_ptr<cmStringReplaceHelper> ReplaceHelper;
  482. };
  483. // Descriptor of action
  484. // Arity: number of arguments required for the action
  485. // Transform: Object implementing the action
  486. struct ActionDescriptor
  487. {
  488. ActionDescriptor(cmList::TransformAction action)
  489. : Action(action)
  490. {
  491. }
  492. ActionDescriptor(cmList::TransformAction action, std::string name,
  493. std::size_t arity,
  494. std::unique_ptr<TransformAction> transform)
  495. : Action(action)
  496. , Name(std::move(name))
  497. , Arity(arity)
  498. , Transform(std::move(transform))
  499. {
  500. }
  501. operator cmList::TransformAction() const { return this->Action; }
  502. cmList::TransformAction Action;
  503. std::string Name;
  504. std::size_t Arity = 0;
  505. std::unique_ptr<TransformAction> Transform;
  506. };
  507. // Build a set of supported actions.
  508. using ActionDescriptorSet = std::set<
  509. ActionDescriptor,
  510. std::function<bool(cmList::TransformAction, cmList::TransformAction)>>;
  511. ActionDescriptorSet Descriptors([](cmList::TransformAction x,
  512. cmList::TransformAction y) {
  513. return x < y;
  514. });
  515. ActionDescriptorSet::iterator TransformConfigure(
  516. cmList::TransformAction action,
  517. std::unique_ptr<cmList::TransformSelector>& selector, std::size_t arity)
  518. {
  519. if (Descriptors.empty()) {
  520. Descriptors.emplace(cmList::TransformAction::APPEND, "APPEND", 1,
  521. cm::make_unique<TransformActionAppend>());
  522. Descriptors.emplace(cmList::TransformAction::PREPEND, "PREPEND", 1,
  523. cm::make_unique<TransformActionPrepend>());
  524. Descriptors.emplace(cmList::TransformAction::TOUPPER, "TOUPPER", 0,
  525. cm::make_unique<TransformActionToUpper>());
  526. Descriptors.emplace(cmList::TransformAction::TOLOWER, "TOLOWER", 0,
  527. cm::make_unique<TransformActionToLower>());
  528. Descriptors.emplace(cmList::TransformAction::STRIP, "STRIP", 0,
  529. cm::make_unique<TransformActionStrip>());
  530. Descriptors.emplace(cmList::TransformAction::GENEX_STRIP, "GENEX_STRIP", 0,
  531. cm::make_unique<TransformActionGenexStrip>());
  532. Descriptors.emplace(cmList::TransformAction::REPLACE, "REPLACE", 2,
  533. cm::make_unique<TransformActionReplace>());
  534. }
  535. auto descriptor = Descriptors.find(action);
  536. if (descriptor == Descriptors.end()) {
  537. throw transform_error(cmStrCat(" sub-command TRANSFORM, ",
  538. std::to_string(static_cast<int>(action)),
  539. " invalid action."));
  540. }
  541. if (descriptor->Arity != arity) {
  542. throw transform_error(cmStrCat("sub-command TRANSFORM, action ",
  543. descriptor->Name, " expects ",
  544. descriptor->Arity, " argument(s)."));
  545. }
  546. if (!selector) {
  547. selector = cm::make_unique<TransformNoSelector>();
  548. }
  549. return descriptor;
  550. }
  551. }
  552. std::unique_ptr<cmList::TransformSelector> cmList::TransformSelector::NewAT(
  553. std::initializer_list<index_type> indexes)
  554. {
  555. return cm::make_unique<TransformSelectorAt>(
  556. std::vector<index_type>{ indexes.begin(), indexes.end() });
  557. ;
  558. }
  559. std::unique_ptr<cmList::TransformSelector> cmList::TransformSelector::NewAT(
  560. std::vector<index_type> const& indexes)
  561. {
  562. return cm::make_unique<TransformSelectorAt>(indexes);
  563. }
  564. std::unique_ptr<cmList::TransformSelector> cmList::TransformSelector::NewAT(
  565. std::vector<index_type>&& indexes)
  566. {
  567. return cm::make_unique<TransformSelectorAt>(std::move(indexes));
  568. }
  569. std::unique_ptr<cmList::TransformSelector> cmList::TransformSelector::NewFOR(
  570. std::initializer_list<index_type> indexes)
  571. {
  572. if (indexes.size() < 2 || indexes.size() > 3) {
  573. throw transform_error("sub-command TRANSFORM, selector FOR "
  574. "expects 2 or 3 arguments");
  575. }
  576. if (indexes.size() == 3 && *(indexes.begin() + 2) < 0) {
  577. throw transform_error("sub-command TRANSFORM, selector FOR expects "
  578. "positive numeric value for <step>.");
  579. }
  580. return cm::make_unique<TransformSelectorFor>(
  581. *indexes.begin(), *(indexes.begin() + 1),
  582. indexes.size() == 3 ? *(indexes.begin() + 2) : 1);
  583. }
  584. std::unique_ptr<cmList::TransformSelector> cmList::TransformSelector::NewFOR(
  585. std::vector<index_type> const& indexes)
  586. {
  587. if (indexes.size() < 2 || indexes.size() > 3) {
  588. throw transform_error("sub-command TRANSFORM, selector FOR "
  589. "expects 2 or 3 arguments");
  590. }
  591. if (indexes.size() == 3 && indexes[2] < 0) {
  592. throw transform_error("sub-command TRANSFORM, selector FOR expects "
  593. "positive numeric value for <step>.");
  594. }
  595. return cm::make_unique<TransformSelectorFor>(
  596. indexes[0], indexes[1], indexes.size() == 3 ? indexes[2] : 1);
  597. }
  598. std::unique_ptr<cmList::TransformSelector> cmList::TransformSelector::NewFOR(
  599. std::vector<index_type>&& indexes)
  600. {
  601. if (indexes.size() < 2 || indexes.size() > 3) {
  602. throw transform_error("sub-command TRANSFORM, selector FOR "
  603. "expects 2 or 3 arguments");
  604. }
  605. if (indexes.size() == 3 && indexes[2] < 0) {
  606. throw transform_error("sub-command TRANSFORM, selector FOR expects "
  607. "positive numeric value for <step>.");
  608. }
  609. return cm::make_unique<TransformSelectorFor>(
  610. indexes[0], indexes[1], indexes.size() == 3 ? indexes[2] : 1);
  611. }
  612. std::unique_ptr<cmList::TransformSelector> cmList::TransformSelector::NewREGEX(
  613. std::string const& regex)
  614. {
  615. std::unique_ptr<::TransformSelector> selector =
  616. cm::make_unique<TransformSelectorRegex>(regex);
  617. if (!selector->Validate()) {
  618. throw transform_error(
  619. cmStrCat("sub-command TRANSFORM, selector REGEX failed to compile "
  620. "regex \"",
  621. regex, "\"."));
  622. }
  623. // weird construct to please all compilers
  624. return std::unique_ptr<cmList::TransformSelector>(selector.release());
  625. }
  626. std::unique_ptr<cmList::TransformSelector> cmList::TransformSelector::NewREGEX(
  627. std::string&& regex)
  628. {
  629. std::unique_ptr<::TransformSelector> selector =
  630. cm::make_unique<TransformSelectorRegex>(std::move(regex));
  631. if (!selector->Validate()) {
  632. throw transform_error(
  633. cmStrCat("sub-command TRANSFORM, selector REGEX failed to compile "
  634. "regex \"",
  635. regex, "\"."));
  636. }
  637. // weird construct to please all compilers
  638. return std::unique_ptr<cmList::TransformSelector>(selector.release());
  639. }
  640. cmList& cmList::transform(TransformAction action,
  641. std::unique_ptr<TransformSelector> selector)
  642. {
  643. auto descriptor = TransformConfigure(action, selector, 0);
  644. descriptor->Transform->Initialize(
  645. static_cast<::TransformSelector*>(selector.get()));
  646. static_cast<::TransformSelector&>(*selector).Transform(
  647. this->Values, [&descriptor](const std::string& s) -> std::string {
  648. return (*descriptor->Transform)(s);
  649. });
  650. return *this;
  651. }
  652. cmList& cmList::transform(TransformAction action, std::string const& arg,
  653. std::unique_ptr<TransformSelector> selector)
  654. {
  655. auto descriptor = TransformConfigure(action, selector, 1);
  656. descriptor->Transform->Initialize(
  657. static_cast<::TransformSelector*>(selector.get()), arg);
  658. static_cast<::TransformSelector&>(*selector).Transform(
  659. this->Values, [&descriptor](const std::string& s) -> std::string {
  660. return (*descriptor->Transform)(s);
  661. });
  662. return *this;
  663. }
  664. cmList& cmList::transform(TransformAction action, std::string const& arg1,
  665. std::string const& arg2,
  666. std::unique_ptr<TransformSelector> selector)
  667. {
  668. auto descriptor = TransformConfigure(action, selector, 2);
  669. descriptor->Transform->Initialize(
  670. static_cast<::TransformSelector*>(selector.get()), arg1, arg2);
  671. static_cast<::TransformSelector&>(*selector).Transform(
  672. this->Values, [&descriptor](const std::string& s) -> std::string {
  673. return (*descriptor->Transform)(s);
  674. });
  675. return *this;
  676. }
  677. cmList& cmList::transform(TransformAction action,
  678. std::vector<std::string> const& args,
  679. std::unique_ptr<TransformSelector> selector)
  680. {
  681. auto descriptor = TransformConfigure(action, selector, args.size());
  682. descriptor->Transform->Initialize(
  683. static_cast<::TransformSelector*>(selector.get()), args);
  684. static_cast<::TransformSelector&>(*selector).Transform(
  685. this->Values, [&descriptor](const std::string& s) -> std::string {
  686. return (*descriptor->Transform)(s);
  687. });
  688. return *this;
  689. }
  690. std::string& cmList::append(std::string& list, std::string&& value)
  691. {
  692. if (list.empty()) {
  693. list = std::move(value);
  694. } else {
  695. list += cmStrCat(cmList::element_separator, value);
  696. }
  697. return list;
  698. }
  699. std::string& cmList::append(std::string& list, cm::string_view value)
  700. {
  701. return cmList::append(list, std::string{ value });
  702. }
  703. std::string& cmList::prepend(std::string& list, std::string&& value)
  704. {
  705. if (list.empty()) {
  706. list = std::move(value);
  707. } else {
  708. list.insert(0, cmStrCat(value, cmList::element_separator));
  709. }
  710. return list;
  711. }
  712. std::string& cmList::prepend(std::string& list, cm::string_view value)
  713. {
  714. return cmList::prepend(list, std::string{ value });
  715. }
  716. cmList::size_type cmList::ComputeIndex(index_type pos, bool boundCheck) const
  717. {
  718. if (boundCheck) {
  719. if (this->Values.empty()) {
  720. throw std::out_of_range(
  721. cmStrCat("index: ", pos, " out of range (0, 0)"));
  722. }
  723. auto index = pos;
  724. if (!this->Values.empty()) {
  725. auto length = this->Values.size();
  726. if (index < 0) {
  727. index = static_cast<index_type>(length) + index;
  728. }
  729. if (index < 0 || length <= static_cast<size_type>(index)) {
  730. throw std::out_of_range(cmStrCat("index: ", pos, " out of range (-",
  731. this->Values.size(), ", ",
  732. this->Values.size() - 1, ")"));
  733. }
  734. }
  735. return index;
  736. }
  737. return pos < 0 ? this->Values.size() + pos : pos;
  738. }
  739. cmList::size_type cmList::ComputeInsertIndex(index_type pos,
  740. bool boundCheck) const
  741. {
  742. if (boundCheck) {
  743. if (this->Values.empty() && pos != 0) {
  744. throw std::out_of_range(
  745. cmStrCat("index: ", pos, " out of range (0, 0)"));
  746. }
  747. auto index = pos;
  748. if (!this->Values.empty()) {
  749. auto length = this->Values.size();
  750. if (index < 0) {
  751. index = static_cast<index_type>(length) + index;
  752. }
  753. if (index < 0 || length < static_cast<size_type>(index)) {
  754. throw std::out_of_range(cmStrCat("index: ", pos, " out of range (-",
  755. this->Values.size(), ", ",
  756. this->Values.size(), ")"));
  757. }
  758. }
  759. return index;
  760. }
  761. return pos < 0 ? this->Values.size() + pos : pos;
  762. }
  763. cmList cmList::GetItems(std::vector<index_type>&& indexes) const
  764. {
  765. cmList listItems;
  766. for (auto index : indexes) {
  767. listItems.emplace_back(this->get_item(index));
  768. }
  769. return listItems;
  770. }
  771. cmList& cmList::RemoveItems(std::vector<index_type>&& indexes)
  772. {
  773. if (indexes.empty()) {
  774. return *this;
  775. }
  776. // compute all indexes
  777. std::vector<size_type> idx(indexes.size());
  778. std::transform(indexes.cbegin(), indexes.cend(), idx.begin(),
  779. [this](const index_type& index) -> size_type {
  780. return this->ComputeIndex(index);
  781. });
  782. std::sort(idx.begin(), idx.end(),
  783. [](size_type l, size_type r) { return l > r; });
  784. auto newEnd = std::unique(idx.begin(), idx.end());
  785. idx.erase(newEnd, idx.end());
  786. for (auto index : idx) {
  787. this->erase(this->begin() + index);
  788. }
  789. return *this;
  790. }
  791. cmList& cmList::RemoveItems(std::vector<std::string>&& items)
  792. {
  793. std::sort(items.begin(), items.end());
  794. auto last = std::unique(items.begin(), items.end());
  795. auto first = items.begin();
  796. auto newEnd = cmRemoveMatching(this->Values, cmMakeRange(first, last));
  797. this->Values.erase(newEnd, this->Values.end());
  798. return *this;
  799. }
  800. cmList::container_type::iterator cmList::Insert(
  801. container_type& container, container_type::const_iterator pos,
  802. std::string&& value, ExpandElements expandElements,
  803. EmptyElements emptyElements)
  804. {
  805. auto delta = std::distance(container.cbegin(), pos);
  806. auto insertPos = container.begin() + delta;
  807. if (expandElements == ExpandElements::Yes) {
  808. // If argument is empty, it is an empty list.
  809. if (emptyElements == EmptyElements::No && value.empty()) {
  810. return insertPos;
  811. }
  812. // if there are no ; in the name then just copy the current string
  813. if (value.find(';') == std::string::npos) {
  814. return container.insert(insertPos, std::move(value));
  815. }
  816. std::string newValue;
  817. // Break the string at non-escaped semicolons not nested in [].
  818. int squareNesting = 0;
  819. auto last = value.begin();
  820. auto const cend = value.end();
  821. for (auto c = last; c != cend; ++c) {
  822. switch (*c) {
  823. case '\\': {
  824. // We only want to allow escaping of semicolons. Other
  825. // escapes should not be processed here.
  826. auto cnext = c + 1;
  827. if ((cnext != cend) && *cnext == ';') {
  828. newValue.append(last, c);
  829. // Skip over the escape character
  830. last = cnext;
  831. c = cnext;
  832. }
  833. } break;
  834. case '[': {
  835. ++squareNesting;
  836. } break;
  837. case ']': {
  838. --squareNesting;
  839. } break;
  840. case ';': {
  841. // brackets.
  842. if (squareNesting == 0) {
  843. newValue.append(last, c);
  844. // Skip over the semicolon
  845. last = c + 1;
  846. if (!newValue.empty() || emptyElements == EmptyElements::Yes) {
  847. // Add the last argument.
  848. insertPos = container.insert(insertPos, newValue);
  849. insertPos++;
  850. newValue.clear();
  851. }
  852. }
  853. } break;
  854. default: {
  855. // Just append this character.
  856. } break;
  857. }
  858. }
  859. newValue.append(last, cend);
  860. if (!newValue.empty() || emptyElements == EmptyElements::Yes) {
  861. // Add the last argument.
  862. container.insert(insertPos, std::move(newValue));
  863. }
  864. } else if (!value.empty() || emptyElements == EmptyElements::Yes) {
  865. return container.insert(insertPos, std::move(value));
  866. }
  867. return container.begin() + delta;
  868. }
  869. std::string const& cmList::ToString(BT<std::string> const& s)
  870. {
  871. return s.Value;
  872. }