1. my class Range { ... }
  2. my class Match { ... }
  3. my class X::Str::InvalidCharName { ... }
  4. my class X::Str::Numeric { ... }
  5. my class X::Str::Match::x { ... }
  6. my class X::Str::Subst::Adverb { ... }
  7. my class X::Str::Trans::IllegalKey { ... }
  8. my class X::Str::Trans::InvalidArg { ... }
  9. my class X::Numeric::Confused { ... }
  10. my class X::Syntax::Number::RadixOutOfRange { ... }
  11. my constant $?TABSTOP = 8;
  12. my class Str does Stringy { # declared in BOOTSTRAP
  13. # class Str is Cool
  14. # has str $!value is box_target;
  15. my $empty := nqp::list; # for nqp::splice
  16. multi method WHY('Life, the Universe and Everything': --> 42) { }
  17. multi method WHICH(Str:D:) {
  18. nqp::box_s(
  19. nqp::concat(
  20. nqp::if(
  21. nqp::eqaddr(self.WHAT,Str),
  22. 'Str|',
  23. nqp::concat(nqp::unbox_s(self.^name), '|')
  24. ),
  25. $!value
  26. ),
  27. ObjAt
  28. )
  29. }
  30. submethod BUILD(Str() :$value = '' --> Nil) {
  31. nqp::bindattr_s(self, Str, '$!value', nqp::unbox_s($value))
  32. }
  33. multi method Bool(Str:D:) {
  34. nqp::p6bool(nqp::chars($!value));
  35. }
  36. multi method Str(Str:D:) { self }
  37. multi method Stringy(Str:D:) { self }
  38. multi method DUMP(Str:D:) { self.perl }
  39. method Int(Str:D:) {
  40. nqp::if(
  41. nqp::isge_i(
  42. nqp::findnotcclass(
  43. nqp::const::CCLASS_NUMERIC,$!value,0,nqp::chars($!value)),
  44. nqp::chars($!value)
  45. )
  46. # Compare Str.chars == Str.codes to filter out any combining characters
  47. && nqp::iseq_i(
  48. nqp::chars($!value),
  49. nqp::elems(
  50. nqp::strtocodes(
  51. $!value,
  52. nqp::const::NORMALIZE_NFC,
  53. nqp::create(NFC),
  54. )
  55. ),
  56. )
  57. ,
  58. nqp::atpos(nqp::radix_I(10,$!value,0,0,Int),0), # all numeric chars
  59. nqp::if(
  60. nqp::istype((my $numeric := self.Numeric),Failure),
  61. $numeric,
  62. $numeric.Int
  63. )
  64. )
  65. }
  66. method Num(Str:D:) {
  67. nqp::if(
  68. nqp::istype((my $numeric := self.Numeric),Failure),
  69. $numeric,
  70. $numeric.Num || nqp::if(
  71. # handle sign of zero. While self.Numeric will give correctly
  72. # signed zero for nums in strings, it won't for other types,
  73. # and since this method is `Num` we want to return proper zero.
  74. # Find first non-whitespace char and check whether it is one
  75. # of the minuses.
  76. nqp::chars(self)
  77. && (
  78. nqp::iseq_i(
  79. (my $ch := nqp::ord(
  80. nqp::substr(
  81. self,
  82. nqp::findnotcclass(
  83. nqp::const::CCLASS_WHITESPACE, self, 0,
  84. nqp::sub_i(nqp::chars(self), 1)
  85. ),
  86. 1,
  87. )
  88. )),
  89. 45, # '-' minus
  90. ) || nqp::iseq_i($ch, 8722) # '−' minus
  91. ),
  92. -0e0,
  93. 0e0
  94. )
  95. )
  96. }
  97. multi method ACCEPTS(Str:D: Str:D \other) {
  98. nqp::p6bool(nqp::iseq_s(nqp::unbox_s(other),$!value));
  99. }
  100. multi method ACCEPTS(Str:D: Any:D \other) {
  101. nqp::p6bool(nqp::iseq_s(nqp::unbox_s(other.Str),$!value));
  102. }
  103. method chomp(Str:D:) {
  104. nqp::if(
  105. (nqp::isge_i((my int $chars = nqp::sub_i(nqp::chars($!value),1)),0)
  106. && nqp::iscclass(nqp::const::CCLASS_NEWLINE,$!value,$chars)),
  107. nqp::p6box_s(nqp::substr($!value,0,$chars)),
  108. self
  109. )
  110. }
  111. multi method chop(Str:D:) {
  112. nqp::if(
  113. nqp::isgt_i(nqp::chars($!value),0),
  114. nqp::p6box_s(
  115. nqp::substr($!value,0,nqp::sub_i(nqp::chars($!value),1))),
  116. ''
  117. )
  118. }
  119. multi method chop(Str:D: Int() $chopping) {
  120. my Int $chars = nqp::chars($!value) - $chopping;
  121. $chars > 0 ?? nqp::p6box_s(nqp::substr($!value,0,$chars)) !! '';
  122. }
  123. # TODO Use coercer in 1 candidate when RT131014
  124. proto method starts-with(|) {*}
  125. multi method starts-with(Str:D: Cool:D $needle) {self.starts-with: $needle.Str}
  126. multi method starts-with(Str:D: Str:D $needle) {
  127. nqp::p6bool(nqp::eqat(self, $needle, 0))
  128. }
  129. # TODO Use coercer in 1 candidate when RT131014
  130. proto method ends-with(|) {*}
  131. multi method ends-with(Str:D: Cool:D $suffix) {self.ends-with: $suffix.Str}
  132. multi method ends-with(Str:D: Str:D $suffix) {
  133. nqp::p6bool(nqp::eqat(
  134. $!value,
  135. nqp::getattr($suffix,Str,'$!value'),
  136. nqp::chars($!value) - nqp::chars(nqp::getattr($suffix,Str,'$!value'))
  137. ))
  138. }
  139. # TODO Use coercer in 1 candidate when RT131014
  140. proto method substr-eq(|) {*}
  141. multi method substr-eq(Str:D: Cool:D $needle) {self.substr-eq: $needle.Str}
  142. multi method substr-eq(Str:D: Str:D $needle) {
  143. nqp::p6bool(nqp::eqat($!value,nqp::getattr($needle,Str,'$!value'),0))
  144. }
  145. multi method substr-eq(Str:D: Cool:D $needle, Int:D $pos) {self.substr-eq: $needle.Str, $pos.Int}
  146. multi method substr-eq(Str:D: Str:D $needle, Int:D $pos) {
  147. nqp::p6bool(
  148. nqp::if(
  149. (nqp::isge_i($pos,0) && nqp::islt_i($pos,nqp::chars($!value))),
  150. nqp::eqat($!value,nqp::getattr($needle,Str,'$!value'),$pos)
  151. )
  152. )
  153. }
  154. # TODO Use coercer in 1 candidate when RT131014
  155. proto method contains(|) {*}
  156. multi method contains(Str:D: Cool:D $needle) {self.contains: $needle.Str}
  157. multi method contains(Str:D: Str:D $needle) {
  158. nqp::p6bool(nqp::isne_i(
  159. nqp::index($!value,nqp::getattr($needle,Str,'$!value'),0),-1
  160. ))
  161. }
  162. multi method contains(Str:D: Cool:D $needle, Int(Cool:D) $pos) {self.contains: $needle.Str, $pos}
  163. multi method contains(Str:D: Str:D $needle, Int:D $pos) {
  164. nqp::p6bool(
  165. nqp::if(
  166. (nqp::isge_i($pos,0) && nqp::islt_i($pos,nqp::chars($!value))),
  167. nqp::isne_i(
  168. nqp::index($!value,nqp::getattr($needle,Str,'$!value'),$pos),-1)
  169. )
  170. )
  171. }
  172. # TODO Use coercer in 1 candidate when RT131014
  173. proto method indices(|) {*}
  174. multi method indices(Str:D: Cool:D $needle, *%pars) {self.indices: $needle.Str, |%pars}
  175. multi method indices(Str:D: Str:D $needle, :$overlap) {
  176. nqp::stmts(
  177. (my $need := nqp::getattr($needle,Str,'$!value')),
  178. (my int $add = nqp::if($overlap,1,nqp::chars($need) || 1)),
  179. (my $indices := nqp::create(IterationBuffer)),
  180. (my int $pos),
  181. (my int $i),
  182. nqp::while(
  183. nqp::isge_i(($i = nqp::index($!value,$need,$pos)),0),
  184. nqp::stmts(
  185. nqp::push($indices,nqp::p6box_i($i)),
  186. ($pos = nqp::add_i($i,$add))
  187. )
  188. ),
  189. nqp::p6bindattrinvres(nqp::create(List),List,'$!reified',$indices)
  190. )
  191. }
  192. multi method indices(Str:D: Cool:D $needle, Cool:D $start, *%pars) {self.indices: $needle.Str, $start.Int, |%pars}
  193. multi method indices(Str:D: Str:D $needle, Int:D $start, :$overlap) {
  194. nqp::stmts(
  195. (my int $pos = $start),
  196. nqp::if(
  197. nqp::isgt_i($pos,nqp::chars($!value)),
  198. nqp::create(List), # position after string, always empty List
  199. nqp::stmts(
  200. (my $need := nqp::getattr($needle,Str,'$!value')),
  201. (my int $add = nqp::if($overlap,1,nqp::chars($need) || 1)),
  202. (my $indices := nqp::create(IterationBuffer)),
  203. (my int $i),
  204. nqp::while(
  205. nqp::isge_i(($i = nqp::index($!value,$need,$pos)),0),
  206. nqp::stmts(
  207. nqp::push($indices,nqp::p6box_i($i)),
  208. ($pos = nqp::add_i($i,$add))
  209. )
  210. ),
  211. nqp::p6bindattrinvres(nqp::create(List),List,'$!reified',$indices)
  212. )
  213. )
  214. )
  215. }
  216. # TODO Use coercer in 1 candidate when RT131014
  217. proto method index(|) {*}
  218. multi method index(Str:D: Cool:D $needle) {self.index: $needle.Str}
  219. multi method index(Str:D: Str:D $needle) {
  220. nqp::if(
  221. nqp::islt_i((my int $i =
  222. nqp::index($!value,nqp::getattr($needle,Str,'$!value'))),
  223. 0
  224. ),
  225. Nil,
  226. nqp::p6box_i($i)
  227. )
  228. }
  229. multi method index(Str:D: Cool:D $needle, Cool:D $pos) {self.index: $needle.Str, $pos.Int}
  230. multi method index(Str:D: Str:D $needle, Int:D $pos) {
  231. nqp::if(
  232. nqp::isbig_I(nqp::decont($pos)),
  233. Failure.new(X::OutOfRange.new(
  234. :what("Position in index"),
  235. :got($pos),
  236. :range("0..{self.chars}")
  237. )),
  238. nqp::if(
  239. nqp::islt_i($pos,0),
  240. Failure.new(X::OutOfRange.new(
  241. :what("Position in index"),
  242. :got($pos),
  243. :range("0..{self.chars}")
  244. )),
  245. nqp::if(
  246. nqp::islt_i((my int $i = nqp::index(
  247. $!value,nqp::getattr($needle,Str,'$!value'),$pos
  248. )),0),
  249. Nil,
  250. nqp::p6box_i($i)
  251. )
  252. )
  253. )
  254. }
  255. # TODO Use coercer in 1 candidate when RT131014
  256. proto method rindex(|) {*}
  257. multi method rindex(Str:D: Cool:D $needle) {self.rindex: $needle.Str}
  258. multi method rindex(Str:D: Str:D $needle) {
  259. nqp::if(
  260. nqp::islt_i((my int $i =
  261. nqp::rindex($!value,nqp::getattr($needle,Str,'$!value'))),
  262. 0
  263. ),
  264. Nil,
  265. nqp::p6box_i($i)
  266. )
  267. }
  268. multi method rindex(Str:D: Cool:D $needle, Cool:D $pos) {self.rindex: $needle.Str, $pos.Int}
  269. multi method rindex(Str:D: Str:D $needle, Int:D $pos) {
  270. nqp::if(
  271. nqp::isbig_I(nqp::decont($pos)),
  272. Failure.new(X::OutOfRange.new(
  273. :what("Position in rindex"),
  274. :got($pos),
  275. :range("0..{self.chars}")
  276. )),
  277. nqp::if(
  278. nqp::islt_i($pos,0),
  279. Failure.new(X::OutOfRange.new(
  280. :what("Position in rindex"),
  281. :got($pos),
  282. :range("0..{self.chars}")
  283. )),
  284. nqp::if(
  285. nqp::islt_i((my int $i = nqp::rindex(
  286. $!value,nqp::getattr($needle,Str,'$!value'),$pos
  287. )),0),
  288. Nil,
  289. nqp::p6box_i($i)
  290. )
  291. )
  292. )
  293. }
  294. method pred(Str:D:) {
  295. (my int $chars = Rakudo::Internals.POSSIBLE-MAGIC-CHARS(self))
  296. ?? Rakudo::Internals.PRED(self,$chars - 1)
  297. !! self
  298. }
  299. method succ(Str:D:) {
  300. (my int $chars = Rakudo::Internals.POSSIBLE-MAGIC-CHARS(self))
  301. ?? Rakudo::Internals.SUCC(self,$chars - 1)
  302. !! self
  303. }
  304. multi method Numeric(Str:D:) {
  305. # Handle special empty string
  306. self.trim eq ""
  307. ?? 0
  308. !! val(self, :val-or-fail)
  309. }
  310. multi method gist(Str:D:) { self }
  311. multi method perl(Str:D:) {
  312. '"' ~ Rakudo::Internals.PERLIFY-STR(self) ~ '"'
  313. }
  314. proto method comb(|) { * }
  315. multi method comb(Str:D:) {
  316. Seq.new(class :: does Iterator {
  317. has str $!str;
  318. has int $!chars;
  319. has int $!pos;
  320. method !SET-SELF(\string) {
  321. nqp::stmts(
  322. ($!str = nqp::unbox_s(string)),
  323. ($!chars = nqp::chars($!str)),
  324. ($!pos = -1),
  325. self
  326. )
  327. }
  328. method new(\string) {
  329. nqp::if(
  330. string,
  331. nqp::create(self)!SET-SELF(string),
  332. Rakudo::Iterator.Empty
  333. )
  334. }
  335. method pull-one() {
  336. nqp::if(
  337. nqp::islt_i(($!pos = nqp::add_i($!pos,1)),$!chars),
  338. nqp::p6box_s(nqp::substr($!str,$!pos,1)),
  339. IterationEnd
  340. )
  341. }
  342. method count-only() { nqp::p6box_i($!chars) }
  343. method bool-only(--> True) { }
  344. }.new(self));
  345. }
  346. multi method comb(Str:D: Int:D $size is copy, $limit = *) {
  347. my int $inf = nqp::istype($limit,Whatever) || $limit == Inf;
  348. return self.comb if $size <= 1 && $inf;
  349. $size = 1 if $size < 1;
  350. Seq.new(class :: does Iterator {
  351. has str $!str;
  352. has int $!chars;
  353. has int $!size;
  354. has int $!pos;
  355. has int $!max;
  356. has int $!todo;
  357. method !SET-SELF(\string,\size,\limit,\inf) {
  358. nqp::stmts(
  359. ($!str = nqp::unbox_s(string)),
  360. ($!chars = nqp::chars($!str)),
  361. ($!size = 1 max size),
  362. ($!pos = -size),
  363. ($!max = 1 + floor( ( $!chars - 1 ) / $!size )),
  364. ($!todo = (inf ?? $!max !! (0 max limit)) + 1),
  365. self
  366. )
  367. }
  368. method new(\string,\size,\limit,\inf) {
  369. nqp::if(
  370. string,
  371. nqp::create(self)!SET-SELF(string,size,limit,inf),
  372. Rakudo::Iterator.Empty
  373. )
  374. }
  375. method pull-one() {
  376. ($!todo = $!todo - 1) && ($!pos = $!pos + $!size) < $!chars
  377. ?? nqp::p6box_s(nqp::substr($!str, $!pos, $!size))
  378. !! IterationEnd
  379. }
  380. method push-all($target --> IterationEnd) {
  381. my int $todo = $!todo;
  382. my int $pos = $!pos;
  383. my int $size = $!size;
  384. my int $chars = $!chars;
  385. $target.push(nqp::p6box_s(nqp::substr($!str, $pos, $size)))
  386. while ($todo = $todo - 1 ) && ($pos = $pos + $size) < $chars;
  387. $!pos = $!chars;
  388. }
  389. method count-only() { $!max }
  390. method bool-only(--> True) { }
  391. }.new(self,$size,$limit,$inf))
  392. }
  393. multi method comb(Str:D: Str $pat) {
  394. return self.comb(1) unless $pat;
  395. Seq.new(class :: does Iterator {
  396. has str $!str;
  397. has str $!pat;
  398. has int $!pos;
  399. method !SET-SELF(\string, \pat) {
  400. $!str = nqp::unbox_s(string);
  401. $!pat = nqp::unbox_s(pat);
  402. self
  403. }
  404. method new(\string, \pat) { nqp::create(self)!SET-SELF(string,pat) }
  405. method pull-one() {
  406. my int $found = nqp::index($!str, $!pat, $!pos);
  407. if $found < 0 {
  408. IterationEnd
  409. }
  410. else {
  411. $!pos = $found + 1;
  412. nqp::p6box_s($!pat)
  413. }
  414. }
  415. }.new(self, $pat));
  416. }
  417. multi method comb(Str:D: Str $pat, $limit) {
  418. return self.comb($pat)
  419. if nqp::istype($limit,Whatever) || $limit == Inf;
  420. return self.comb(1, $limit) unless $pat;
  421. Seq.new(class :: does Iterator {
  422. has str $!str;
  423. has str $!pat;
  424. has int $!pos;
  425. has int $!todo;
  426. method !SET-SELF(\string, \pat, \limit) {
  427. $!str = nqp::unbox_s(string);
  428. $!pat = nqp::unbox_s(pat);
  429. $!todo = nqp::unbox_i(limit.Int);
  430. self
  431. }
  432. method new(\string, \pat, \limit) {
  433. nqp::create(self)!SET-SELF(string, pat, limit)
  434. }
  435. method pull-one() {
  436. my int $found = nqp::index($!str, $!pat, $!pos);
  437. if $found < 0 || $!todo == 0 {
  438. IterationEnd
  439. }
  440. else {
  441. $!pos = $found + 1;
  442. $!todo = $!todo - 1;
  443. nqp::p6box_s($!pat)
  444. }
  445. }
  446. }.new(self, $pat, $limit));
  447. }
  448. multi method comb(Str:D: Regex:D $pattern, :$match) {
  449. nqp::if(
  450. $match,
  451. self.match($pattern, :g),
  452. self.match($pattern, :g, :as(Str))
  453. )
  454. }
  455. multi method comb(Str:D: Regex:D $pattern, $limit, :$match) {
  456. nqp::if(
  457. nqp::istype($limit,Whatever) || $limit == Inf,
  458. self.comb($pattern, :$match),
  459. nqp::if(
  460. $match,
  461. self.match($pattern, :x(1..$limit)),
  462. self.match($pattern, :x(1..$limit), :as(Str))
  463. )
  464. )
  465. }
  466. # cache cursor initialization lookup
  467. my $cursor-init := Match.^lookup("!cursor_init");
  468. my \CURSOR-GLOBAL := Match.^lookup("CURSOR_MORE" ); # :g
  469. my \CURSOR-OVERLAP := Match.^lookup("CURSOR_OVERLAP"); # :ov
  470. my \CURSOR-EXHAUSTIVE := Match.^lookup("CURSOR_NEXT" ); # :ex
  471. my \POST-MATCH := Match.^lookup("MATCH" ); # Match object
  472. my \POST-STR := Match.^lookup("STR" ); # Str object
  473. # iterate with post-processing
  474. class POST-ITERATOR does Iterator {
  475. has Mu $!cursor; # cannot put these 3 lines in role
  476. has Mu $!move;
  477. has Mu $!post;
  478. method !SET-SELF(\cursor,\move,\post) {
  479. $!cursor := cursor;
  480. $!move := move;
  481. $!post := post;
  482. self
  483. }
  484. method new(\c,\t,\p) { nqp::create(self)!SET-SELF(c,t,p) }
  485. method pull-one() is raw {
  486. nqp::if(
  487. nqp::isge_i(nqp::getattr_i($!cursor,Match,'$!pos'),0),
  488. nqp::stmts(
  489. (my $pulled := $!cursor),
  490. ($!cursor := $!move($!cursor)),
  491. $!post($pulled)
  492. ),
  493. IterationEnd
  494. )
  495. }
  496. method skip-one() is raw {
  497. nqp::if(
  498. nqp::isge_i(nqp::getattr_i($!cursor,Match,'$!pos'),0),
  499. ($!cursor := $!move($!cursor)),
  500. )
  501. }
  502. method push-all($target --> IterationEnd) {
  503. nqp::while(
  504. nqp::isge_i(nqp::getattr_i($!cursor,Match,'$!pos'),0),
  505. nqp::stmts(
  506. $target.push($!post($!cursor)),
  507. ($!cursor := $!move($!cursor))
  508. )
  509. )
  510. }
  511. }
  512. # iterate returning Matches
  513. class CURSOR-ITERATOR does Iterator {
  514. has Mu $!cursor;
  515. has Mu $!move;
  516. method !SET-SELF(\cursor,\move) {
  517. $!cursor := cursor;
  518. $!move := move;
  519. self
  520. }
  521. method new(\c,\t) { nqp::create(self)!SET-SELF(c,t) }
  522. method pull-one() is raw {
  523. nqp::if(
  524. nqp::isge_i(nqp::getattr_i($!cursor,Match,'$!pos'),0),
  525. nqp::stmts(
  526. (my $pulled := $!cursor),
  527. ($!cursor := $!move($!cursor)),
  528. $pulled
  529. ),
  530. IterationEnd
  531. )
  532. }
  533. method skip-one() is raw {
  534. nqp::if(
  535. nqp::isge_i(nqp::getattr_i($!cursor,Match,'$!pos'),0),
  536. ($!cursor := $!move($!cursor)),
  537. )
  538. }
  539. method push-all($target --> IterationEnd) {
  540. nqp::while(
  541. nqp::isge_i(nqp::getattr_i($!cursor,Match,'$!pos'),0),
  542. nqp::stmts(
  543. $target.push($!cursor),
  544. ($!cursor := $!move($!cursor))
  545. )
  546. )
  547. }
  548. }
  549. # Look for short/long named parameter and remove it from the hash
  550. sub fetch-short-long(\opts, str $short, str $long, \store --> Nil) {
  551. nqp::if(
  552. nqp::existskey(opts,$short),
  553. nqp::stmts(
  554. (store = nqp::atkey(opts,$short)),
  555. nqp::deletekey(opts,$short)
  556. ),
  557. nqp::if(
  558. nqp::existskey(opts,$long),
  559. nqp::stmts(
  560. (store = nqp::atkey(opts,$long)),
  561. nqp::deletekey(opts,$long)
  562. )
  563. )
  564. )
  565. }
  566. # Look for named parameters, do not remove from hash
  567. sub fetch-all-of(\opts, @names, \store --> Nil) {
  568. nqp::stmts(
  569. (my int $elems = @names.elems), # reifies
  570. (my $list := nqp::getattr(@names,List,'$!reified')),
  571. (my int $i = -1),
  572. nqp::while(
  573. nqp::islt_i(($i = nqp::add_i($i,1)),$elems),
  574. nqp::if(
  575. nqp::existskey(opts,nqp::unbox_s(nqp::atpos($list,$i))),
  576. (store = nqp::atkey(opts,nqp::unbox_s(nqp::atpos($list,$i)))),
  577. )
  578. )
  579. )
  580. }
  581. sub die-before-first($got) {
  582. die "Attempt to retrieve before :1st match -- :nth({
  583. $got // $got.^name
  584. })"
  585. }
  586. # All of these !match methods take a nqp::getlexcaller value for the $/
  587. # to be set as the first parameter. The second parameter is usually
  588. # the Match object to be used (or something from which a Match can
  589. # be made).
  590. # Generic fallback for matching with a pattern
  591. method !match-pattern(\slash, $pattern, str $name, $value, \opts) {
  592. nqp::stmts(
  593. (my $opts := nqp::getattr(opts,Map,'$!storage')),
  594. nqp::bindkey($opts,$name,$value),
  595. fetch-short-long($opts, "p", "pos", my $p),
  596. fetch-short-long($opts, "c", "continue", my $c),
  597. nqp::unless(nqp::defined($c), $c = 0),
  598. nqp::if(
  599. nqp::elems($opts),
  600. nqp::if(
  601. nqp::defined($p),
  602. self!match-cursor(slash,
  603. $pattern($cursor-init(Match,self,:$p)), '', 0, $opts),
  604. self!match-cursor(slash,
  605. $pattern($cursor-init(Match,self,:$c)), '', 0, $opts)
  606. ),
  607. nqp::if(
  608. nqp::defined($p),
  609. self!match-one(slash,
  610. $pattern($cursor-init(Match,self,:$p))),
  611. self!match-one(slash,
  612. $pattern($cursor-init(Match,self,:$c)))
  613. )
  614. )
  615. )
  616. }
  617. # Generic fallback for matching with a cursor. This is typically
  618. # called if more than one named parameter was specified. Arguments
  619. # 3/4 are the initial named parameter matched: instead of flattening
  620. # the named parameter into another slurpy hash, we pass the name and
  621. # the value as extra parameters, and add it back in the hash with
  622. # named parameters.
  623. method !match-cursor(\slash, \cursor, str $name, $value, \opts) {
  624. nqp::stmts(
  625. (my $opts := nqp::getattr(opts,Map,'$!storage')),
  626. nqp::if(
  627. nqp::chars($name),
  628. nqp::bindkey($opts,$name,$value)
  629. ),
  630. fetch-short-long($opts, "ex", "exhaustive", my $ex),
  631. fetch-short-long($opts, "ov", "overlap", my $ov),
  632. (my \move := nqp::if($ex, CURSOR-EXHAUSTIVE,
  633. nqp::if($ov, CURSOR-OVERLAP, CURSOR-GLOBAL))),
  634. fetch-short-long($opts, "as", "as", my $as),
  635. (my \post := nqp::if(nqp::istype($as,Str), POST-STR, POST-MATCH)),
  636. fetch-short-long($opts, "g", "global", my $g),
  637. nqp::if(
  638. nqp::elems($opts),
  639. nqp::stmts(
  640. fetch-short-long($opts, "x", "x", my $x),
  641. fetch-all-of($opts, <st nd rd th nth>, my $nth),
  642. nqp::if(
  643. nqp::defined($nth),
  644. nqp::if(
  645. nqp::defined($x), # :nth && :x
  646. self!match-x(slash,
  647. self!match-nth(slash, cursor,
  648. move, post, $nth, nqp::hash).iterator, $x),
  649. self!match-nth(slash, cursor,
  650. move, post, $nth, nqp::hash) # nth
  651. ),
  652. nqp::if(
  653. nqp::defined($x),
  654. self!match-x(slash, # :x
  655. POST-ITERATOR.new(cursor, move, post), $x),
  656. nqp::if( # only :ex|ov|g
  657. $ex || $ov || $g,
  658. self!match-list(slash, cursor, move, post),
  659. self!match-one(slash, cursor)
  660. )
  661. )
  662. )
  663. ),
  664. nqp::if( # only :ex|ov|g
  665. $ex || $ov || $g,
  666. self!match-list(slash, cursor, move, post),
  667. self!match-one(slash, cursor)
  668. )
  669. )
  670. )
  671. }
  672. # Match object at given position
  673. method !match-one(\slash, \cursor) {
  674. nqp::decont(slash = nqp::if(
  675. nqp::isge_i(nqp::getattr_i(cursor,Match,'$!pos'),0),
  676. cursor.MATCH,
  677. Nil
  678. ))
  679. }
  680. # Some object at given position
  681. method !match-as-one(\slash, \cursor, \as) {
  682. nqp::decont(slash = nqp::if(
  683. nqp::isge_i(nqp::getattr_i(cursor,Match,'$!pos'),0),
  684. nqp::if(nqp::istype(as,Str), POST-STR, POST-MATCH)(cursor),
  685. Nil
  686. ))
  687. }
  688. # Create list from the appropriate Sequence given the move
  689. method !match-list(\slash, \cursor, \move, \post) {
  690. nqp::decont(slash = nqp::if(
  691. nqp::isge_i(nqp::getattr_i(cursor,Match,'$!pos'),0),
  692. Seq.new(POST-ITERATOR.new(cursor, move, post)).list,
  693. List.new,
  694. ))
  695. }
  696. # Handle matching of the nth match specification.
  697. method !match-nth(\slash, \cursor, \move, \post, $nth, %opts) {
  698. nqp::if(
  699. nqp::elems(nqp::getattr(%opts,Map,'$!storage')),
  700. self!match-cursor(slash, cursor, 'nth', $nth, %opts),
  701. nqp::if(
  702. nqp::defined($nth),
  703. nqp::if(
  704. nqp::istype($nth,Whatever),
  705. self!match-last(slash, cursor, move),
  706. nqp::if(
  707. nqp::istype($nth,Numeric),
  708. nqp::if(
  709. $nth == Inf,
  710. self!match-last(slash, cursor, move),
  711. nqp::if(
  712. $nth < 1,
  713. die-before-first($nth),
  714. self!match-nth-int(slash, cursor, move, post, $nth.Int)
  715. )
  716. ),
  717. nqp::if(
  718. nqp::istype($nth,WhateverCode),
  719. nqp::if(
  720. nqp::iseq_i((my int $tail = abs($nth(-1))),1),
  721. self!match-last(slash, cursor, move),
  722. self!match-nth-tail(slash, cursor, move, $tail)
  723. ),
  724. nqp::if(
  725. nqp::istype($nth,Callable),
  726. self!match-nth-int(slash,
  727. cursor, move, post, $nth()),
  728. self!match-nth-iterator(slash,
  729. POST-ITERATOR.new(cursor, move, post),
  730. $nth.iterator)
  731. )
  732. )
  733. )
  734. ),
  735. self!match-one(slash, cursor)
  736. )
  737. )
  738. }
  739. # Give back the nth match found
  740. method !match-nth-int(\slash, \cursor, \move, \post, int $nth) {
  741. nqp::decont(slash = nqp::if(
  742. nqp::isge_i(nqp::getattr_i(cursor,Match,'$!pos'),0),
  743. nqp::if(
  744. nqp::eqaddr(
  745. (my $pulled := POST-ITERATOR.new(cursor, move, post)
  746. .skip-at-least-pull-one(nqp::sub_i($nth,1))),
  747. IterationEnd
  748. ),
  749. Nil, # not enough matches
  750. $pulled # found it!
  751. ),
  752. Nil # no matches whatsoever
  753. ))
  754. }
  755. # Give back the N-tail match found
  756. method !match-nth-tail(\slash, \cursor, \move, int $tail) {
  757. nqp::decont(slash = nqp::if(
  758. nqp::eqaddr((my $pulled :=
  759. Rakudo::Iterator.LastNValues(
  760. CURSOR-ITERATOR.new(cursor, move),
  761. $tail, 'match', 1).pull-one),
  762. IterationEnd
  763. ),
  764. Nil,
  765. $pulled.MATCH
  766. ))
  767. }
  768. # Give last value of given iterator, or Nil if none
  769. method !match-last(\slash, \cursor, \move) {
  770. nqp::decont(slash = nqp::if(
  771. nqp::eqaddr((my $pulled :=
  772. Rakudo::Iterator.LastValue(
  773. CURSOR-ITERATOR.new(cursor, move),
  774. 'match')),
  775. IterationEnd
  776. ),
  777. Nil,
  778. $pulled.MATCH
  779. ))
  780. }
  781. # These !match methods take an iterator instead of a cursor.
  782. # Give list with matches found given a range with :nth
  783. method !match-nth-range(\slash, \iterator, $min, $max) {
  784. nqp::decont(slash = nqp::stmts(
  785. (my int $skip = $min),
  786. nqp::if(
  787. nqp::islt_i($skip,1),
  788. die-before-first($min),
  789. nqp::stmts(
  790. nqp::while(
  791. nqp::isgt_i($skip,1) && iterator.skip-one,
  792. ($skip = nqp::sub_i($skip,1))
  793. ),
  794. nqp::if(
  795. nqp::iseq_i($skip,1),
  796. nqp::if( # did not exhaust while skipping
  797. $max == Inf, # * is Inf in N..*
  798. nqp::stmts( # open ended
  799. (my $matches := nqp::create(IterationBuffer)),
  800. nqp::until(
  801. nqp::eqaddr(
  802. (my $pulled := iterator.pull-one),
  803. IterationEnd
  804. ),
  805. nqp::push($matches,$pulled)
  806. ),
  807. nqp::p6bindattrinvres(
  808. nqp::create(List),List,'$!reified',$matches)
  809. ),
  810. nqp::stmts( # upto the max index
  811. (my int $todo = $max - $min + 1),
  812. ($matches :=
  813. nqp::setelems(nqp::create(IterationBuffer),$todo)),
  814. (my int $i = -1),
  815. nqp::until(
  816. nqp::iseq_i(($i = nqp::add_i($i,1)),$todo)
  817. || nqp::eqaddr(
  818. ($pulled := iterator.pull-one),IterationEnd),
  819. nqp::bindpos($matches,$i,$pulled)
  820. ),
  821. nqp::if(
  822. nqp::iseq_i($i,$todo),
  823. nqp::p6bindattrinvres( # found all values
  824. nqp::create(List),List,'$!reified',$matches),
  825. Empty # no match, since not all values
  826. )
  827. )
  828. ),
  829. Empty # exhausted while skipping
  830. )
  831. )
  832. )
  833. ))
  834. }
  835. # Give list with matches found given an iterator with :nth
  836. method !match-nth-iterator(\slash, \source, \indexes) {
  837. nqp::decont(slash = nqp::stmts(
  838. Seq.new(Rakudo::Iterator.MonotonicIndexes(
  839. source, indexes, 1,
  840. -> $got,$next {
  841. nqp::if(
  842. $next == 1,
  843. die-before-first($got),
  844. (die "Attempt to fetch match #$got after #{$next - 1}")
  845. )
  846. }
  847. )).list
  848. ))
  849. }
  850. # Give list with matches found given an iterator with :x
  851. method !match-x(\slash, \iterator, $x) {
  852. nqp::if(
  853. nqp::istype($x,Whatever),
  854. Seq.new(iterator).list,
  855. nqp::if(
  856. nqp::istype($x,Numeric),
  857. nqp::if(
  858. $x == Inf,
  859. Seq.new(iterator).list,
  860. nqp::if(
  861. nqp::istype($x,Int),
  862. self!match-x-range(slash, iterator, $x, $x),
  863. nqp::stmts(
  864. (my int $xint = $x.Int),
  865. self!match-x-range(slash, iterator, $xint, $xint)
  866. )
  867. )
  868. ),
  869. nqp::if(
  870. nqp::istype($x,Range),
  871. self!match-x-range(slash, iterator, $x.min, $x.max),
  872. nqp::stmts(
  873. (slash = Nil),
  874. Failure.new(X::Str::Match::x.new(:got($x)))
  875. )
  876. )
  877. )
  878. )
  879. }
  880. # Give list with matches found given a range with :x
  881. method !match-x-range(\slash, \iterator, $min, $max) {
  882. nqp::decont(slash = nqp::stmts(
  883. (my int $todo = nqp::if($max == Inf, 0x7fffffff, $max)),
  884. (my $matches := nqp::create(IterationBuffer)),
  885. nqp::until(
  886. nqp::islt_i(($todo = nqp::sub_i($todo,1)), 0) ||
  887. nqp::eqaddr((my $pulled := iterator.pull-one),IterationEnd),
  888. nqp::push($matches,$pulled)
  889. ),
  890. nqp::if(
  891. nqp::elems($matches) >= $min,
  892. nqp::p6bindattrinvres(
  893. nqp::create(List),List,'$!reified',$matches),
  894. Empty
  895. )
  896. ))
  897. }
  898. proto method match(|) { $/ := nqp::getlexcaller('$/'); {*} }
  899. multi method match(Cool:D $pattern, |c) {
  900. $/ := nqp::getlexcaller('$/');
  901. self.match(/ "$pattern": /,|c)
  902. }
  903. # All of these .match candidates take a single required named parameter
  904. # so that handling specification of a single named parameter can be much
  905. # quicker. Unfortunately, we cannot cheaply do MMD on an empty slurpy
  906. # hash, which would make things much more simple.
  907. multi method match(Regex:D $pattern, :continue(:$c)!, *%_) {
  908. nqp::if(
  909. nqp::elems(nqp::getattr(%_,Map,'$!storage')),
  910. self!match-pattern(nqp::getlexcaller('$/'), $pattern, 'c', $c, %_),
  911. self!match-one(nqp::getlexcaller('$/'),
  912. $pattern($cursor-init(Match,self,:$c)))
  913. )
  914. }
  915. multi method match(Regex:D $pattern, :pos(:$p)!, *%_) {
  916. nqp::if(
  917. nqp::elems(nqp::getattr(%_,Map,'$!storage')),
  918. self!match-pattern(nqp::getlexcaller('$/'), $pattern, 'p', $p, %_),
  919. nqp::if(
  920. nqp::defined($p),
  921. self!match-one(nqp::getlexcaller('$/'),
  922. $pattern($cursor-init(Match,self,:$p))),
  923. self!match-one(nqp::getlexcaller('$/'),
  924. $pattern($cursor-init(Match,self,:0c)))
  925. )
  926. )
  927. }
  928. multi method match(Regex:D $pattern, :global(:$g)!, *%_) {
  929. nqp::if(
  930. nqp::elems(nqp::getattr(%_,Map,'$!storage')),
  931. self!match-cursor(nqp::getlexcaller('$/'),
  932. $pattern($cursor-init(Match,self,:0c)), 'g', $g, %_),
  933. nqp::if(
  934. $g,
  935. self!match-list(nqp::getlexcaller('$/'),
  936. $pattern($cursor-init(Match,self,:0c)),
  937. CURSOR-GLOBAL, POST-MATCH),
  938. self!match-one(nqp::getlexcaller('$/'),
  939. $pattern($cursor-init(Match,self,:0c)))
  940. )
  941. )
  942. }
  943. multi method match(Regex:D $pattern, :overlap(:$ov)!, *%_) {
  944. nqp::if(
  945. nqp::elems(nqp::getattr(%_,Map,'$!storage')),
  946. self!match-cursor(nqp::getlexcaller('$/'),
  947. $pattern($cursor-init(Match,self,:0c)), 'ov', $ov, %_),
  948. nqp::if(
  949. $ov,
  950. self!match-list(nqp::getlexcaller('$/'),
  951. $pattern($cursor-init(Match,self,:0c)),
  952. CURSOR-OVERLAP, POST-MATCH),
  953. self!match-one(nqp::getlexcaller('$/'),
  954. $pattern($cursor-init(Match,self,:0c)))
  955. )
  956. )
  957. }
  958. multi method match(Regex:D $pattern, :exhaustive(:$ex)!, *%_) {
  959. nqp::if(
  960. nqp::elems(nqp::getattr(%_,Map,'$!storage')),
  961. self!match-cursor(nqp::getlexcaller('$/'),
  962. $pattern($cursor-init(Match,self,:0c)), 'ex', $ex, %_),
  963. nqp::if(
  964. $ex,
  965. self!match-list(nqp::getlexcaller('$/'),
  966. $pattern($cursor-init(Match,self,:0c)),
  967. CURSOR-EXHAUSTIVE, POST-MATCH),
  968. self!match-one(nqp::getlexcaller('$/'),
  969. $pattern($cursor-init(Match,self,:0c)))
  970. )
  971. )
  972. }
  973. multi method match(Regex:D $pattern, :$x!, *%_) {
  974. nqp::if(
  975. nqp::elems(nqp::getattr(%_,Map,'$!storage')),
  976. self!match-cursor(nqp::getlexcaller('$/'),
  977. $pattern($cursor-init(Match,self,:0c)), 'x', $x, %_),
  978. nqp::if(
  979. nqp::defined($x),
  980. self!match-x(nqp::getlexcaller('$/'),
  981. POST-ITERATOR.new($pattern($cursor-init(Match,self,:0c)),
  982. CURSOR-GLOBAL, POST-MATCH
  983. ), $x),
  984. self!match-one(nqp::getlexcaller('$/'),
  985. $pattern($cursor-init(Match,self,:0c)), $x)
  986. )
  987. )
  988. }
  989. multi method match(Regex:D $pattern, :$st!, *%_) {
  990. self!match-nth(nqp::getlexcaller('$/'),
  991. $pattern($cursor-init(Match,self,:0c)),
  992. CURSOR-GLOBAL, POST-MATCH, $st, %_)
  993. }
  994. multi method match(Regex:D $pattern, :$nd!, *%_) {
  995. self!match-nth(nqp::getlexcaller('$/'),
  996. $pattern($cursor-init(Match,self,:0c)),
  997. CURSOR-GLOBAL, POST-MATCH, $nd, %_)
  998. }
  999. multi method match(Regex:D $pattern, :$rd!, *%_) {
  1000. self!match-nth(nqp::getlexcaller('$/'),
  1001. $pattern($cursor-init(Match,self,:0c)),
  1002. CURSOR-GLOBAL, POST-MATCH, $rd, %_)
  1003. }
  1004. multi method match(Regex:D $pattern, :$th!, *%_) {
  1005. self!match-nth(nqp::getlexcaller('$/'),
  1006. $pattern($cursor-init(Match,self,:0c)),
  1007. CURSOR-GLOBAL, POST-MATCH, $th, %_)
  1008. }
  1009. multi method match(Regex:D $pattern, :$nth!, *%_) {
  1010. self!match-nth(nqp::getlexcaller('$/'),
  1011. $pattern($cursor-init(Match,self,:0c)),
  1012. CURSOR-GLOBAL, POST-MATCH, $nth, %_)
  1013. }
  1014. multi method match(Regex:D $pattern, :$as!, *%_) {
  1015. nqp::if(
  1016. nqp::elems(nqp::getattr(%_,Map,'$!storage')),
  1017. self!match-cursor(nqp::getlexcaller('$/'),
  1018. $pattern($cursor-init(Match,self,:0c)), 'as', $as, %_),
  1019. self!match-as-one(nqp::getlexcaller('$/'),
  1020. $pattern($cursor-init(Match,self,:0c)), $as)
  1021. )
  1022. }
  1023. multi method match(Regex:D $pattern, *%_) {
  1024. nqp::if(
  1025. nqp::elems(nqp::getattr(%_,Map,'$!storage')),
  1026. self!match-cursor(nqp::getlexcaller('$/'),
  1027. $pattern($cursor-init(Match,self,:0c)), '', 0, %_),
  1028. self!match-one(nqp::getlexcaller('$/'),
  1029. $pattern($cursor-init(Match,self,:0c)))
  1030. )
  1031. }
  1032. proto method subst-mutate(|) {
  1033. $/ := nqp::getlexcaller('$/');
  1034. {*}
  1035. }
  1036. multi method subst-mutate(
  1037. Str:D $self is rw: Any:D $matcher, $replacement,
  1038. :ii(:$samecase), :ss(:$samespace), :mm(:$samemark), *%options
  1039. ) {
  1040. my $global = %options<g> || %options<global>;
  1041. my $caller_dollar_slash := nqp::getlexcaller('$/');
  1042. my $SET_DOLLAR_SLASH = nqp::istype($matcher, Regex);
  1043. my $word_by_word = so $samespace || %options<s> || %options<sigspace>;
  1044. try $caller_dollar_slash = $/ if $SET_DOLLAR_SLASH;
  1045. my @matches = %options
  1046. ?? self.match($matcher, |%options)
  1047. !! self.match($matcher); # 30% faster
  1048. if nqp::istype(@matches[0], Failure) {
  1049. @matches[0];
  1050. }
  1051. elsif !@matches || (@matches == 1 && !@matches[0]) {
  1052. Nil;
  1053. }
  1054. else {
  1055. $self = $self!APPLY-MATCHES(
  1056. @matches,
  1057. $replacement,
  1058. $caller_dollar_slash,
  1059. $SET_DOLLAR_SLASH,
  1060. $word_by_word,
  1061. $samespace,
  1062. $samecase,
  1063. $samemark,
  1064. );
  1065. nqp::if(
  1066. $global || %options<x>,
  1067. nqp::p6bindattrinvres(
  1068. nqp::create(List),
  1069. List,
  1070. '$!reified',
  1071. nqp::getattr(@matches,List,'$!reified')
  1072. ),
  1073. @matches[0]
  1074. )
  1075. }
  1076. }
  1077. proto method subst(|) {
  1078. $/ := nqp::getlexcaller('$/');
  1079. {*}
  1080. }
  1081. multi method subst(Str:D: $matcher, $replacement, :global(:$g),
  1082. :ii(:$samecase), :ss(:$samespace), :mm(:$samemark),
  1083. *%options) {
  1084. # take the fast lane if we can
  1085. return Rakudo::Internals.TRANSPOSE(self,$matcher,$replacement)
  1086. if nqp::istype($matcher,Str) && nqp::istype($replacement,Str)
  1087. && $g
  1088. && !$samecase && !$samespace && !$samemark && !%options;
  1089. X::Str::Subst::Adverb.new(:name($_), :got(%options{$_})).throw
  1090. if %options{$_} for <ov ex>;
  1091. my $caller_dollar_slash := nqp::getlexcaller('$/');
  1092. my $SET_DOLLAR_SLASH = nqp::istype($matcher, Regex);
  1093. my $word_by_word = so $samespace || %options<s> || %options<sigspace>;
  1094. # nothing to do
  1095. try $caller_dollar_slash = $/ if $SET_DOLLAR_SLASH;
  1096. my @matches = %options
  1097. ?? self.match($matcher, :$g, |%options)
  1098. !! self.match($matcher, :$g); # 30% faster
  1099. nqp::istype(@matches[0], Failure)
  1100. ?? @matches[0]
  1101. !! !@matches || (@matches == 1 && !@matches[0])
  1102. ?? self
  1103. !! self!APPLY-MATCHES(
  1104. @matches,
  1105. $replacement,
  1106. $caller_dollar_slash,
  1107. $SET_DOLLAR_SLASH,
  1108. $word_by_word,
  1109. $samespace,
  1110. $samecase,
  1111. $samemark,
  1112. );
  1113. }
  1114. method !APPLY-MATCHES(\matches,$replacement,\cds,\SDS,\word_by_word,\space,\case,\mark) {
  1115. my \callable := nqp::istype($replacement,Callable);
  1116. my int $prev;
  1117. my str $str = nqp::unbox_s(self);
  1118. my Mu $result := nqp::list_s();
  1119. try cds = $/ if SDS;
  1120. # need to do something special
  1121. if SDS || space || case || mark || callable {
  1122. my \noargs := callable ?? $replacement.count == 0 !! False;
  1123. my \fancy := space || case || mark || word_by_word;
  1124. my \case-and-mark := case && mark;
  1125. for flat matches -> $m {
  1126. try cds = $m if SDS;
  1127. nqp::push_s(
  1128. $result,nqp::substr($str,$prev,nqp::unbox_i($m.from) - $prev)
  1129. );
  1130. if fancy {
  1131. my $mstr := $m.Str;
  1132. my $it := ~(callable
  1133. ?? (noargs ?? $replacement() !! $replacement($m))
  1134. !! $replacement
  1135. );
  1136. if word_by_word { # all spacers delegated to word-by-word
  1137. my &filter :=
  1138. case-and-mark
  1139. ?? -> $w,$p { $w.samemark($p).samecase($p) }
  1140. !! case
  1141. ?? -> $w,$p { $w.samecase($p) }
  1142. !! -> $w,$p { $w.samemark($p) }
  1143. nqp::push_s($result,nqp::unbox_s(
  1144. $it.word-by-word($mstr,&filter,:samespace(?space))
  1145. ) );
  1146. }
  1147. elsif case-and-mark {
  1148. nqp::push_s($result,nqp::unbox_s(
  1149. $it.samecase($mstr).samemark($mstr)
  1150. ) );
  1151. }
  1152. elsif case {
  1153. nqp::push_s($result,nqp::unbox_s($it.samecase(~$m)));
  1154. }
  1155. else { # mark
  1156. nqp::push_s($result,nqp::unbox_s($it.samemark(~$m)));
  1157. }
  1158. }
  1159. else {
  1160. nqp::push_s($result,nqp::unbox_s( ~(callable
  1161. ?? (noargs ?? $replacement() !! $replacement($m))
  1162. !! $replacement
  1163. ) ) );
  1164. }
  1165. $prev = nqp::unbox_i($m.to);
  1166. }
  1167. nqp::push_s($result,nqp::substr($str,$prev));
  1168. nqp::p6box_s(nqp::join('',$result));
  1169. }
  1170. # simple string replacement
  1171. else {
  1172. for flat matches -> $m {
  1173. nqp::push_s(
  1174. $result,nqp::substr($str,$prev,nqp::unbox_i($m.from) - $prev)
  1175. );
  1176. $prev = nqp::unbox_i($m.to);
  1177. }
  1178. nqp::push_s($result,nqp::substr($str,$prev));
  1179. nqp::p6box_s(nqp::join(nqp::unbox_s(~$replacement),$result));
  1180. }
  1181. }
  1182. method ords(Str:D:) { self.NFC.list }
  1183. proto method lines(|) { * }
  1184. multi method lines(Str:D: :$count!) {
  1185. # we should probably deprecate this feature
  1186. $count ?? self.lines.elems !! self.lines;
  1187. }
  1188. multi method lines(Str:D: $limit) {
  1189. nqp::istype($limit,Whatever) || $limit == Inf
  1190. ?? self.lines
  1191. !! self.lines.head($limit)
  1192. }
  1193. multi method lines(Str:D:) {
  1194. Seq.new(class :: does Iterator {
  1195. has str $!str;
  1196. has int $!chars;
  1197. has int $!pos;
  1198. method !SET-SELF(\string) {
  1199. $!str = nqp::unbox_s(string);
  1200. $!chars = nqp::chars($!str);
  1201. $!pos = 0;
  1202. self
  1203. }
  1204. method new(\string) { nqp::create(self)!SET-SELF(string) }
  1205. method pull-one() {
  1206. my int $left;
  1207. return IterationEnd if ($left = $!chars - $!pos) <= 0;
  1208. my int $nextpos = nqp::findcclass(
  1209. nqp::const::CCLASS_NEWLINE, $!str, $!pos, $left);
  1210. my str $found = nqp::substr($!str, $!pos, $nextpos - $!pos);
  1211. $!pos = $nextpos + 1;
  1212. $found;
  1213. }
  1214. method push-all($target --> IterationEnd) {
  1215. my int $left;
  1216. my int $nextpos;
  1217. while ($left = $!chars - $!pos) > 0 {
  1218. $nextpos = nqp::findcclass(
  1219. nqp::const::CCLASS_NEWLINE, $!str, $!pos, $left);
  1220. $target.push(nqp::substr($!str, $!pos, $nextpos - $!pos));
  1221. $!pos = $nextpos + 1;
  1222. }
  1223. }
  1224. }.new(self));
  1225. }
  1226. method !ensure-split-sanity(\v,\k,\kv,\p) {
  1227. # cannot combine these
  1228. my int $any = ?v + ?k + ?kv + ?p;
  1229. X::Adverb.new(
  1230. what => 'split',
  1231. source => 'Str',
  1232. nogo => (:v(v),:k(k),:kv(kv),:p(p)).grep(*.value).map(*.key),
  1233. ).throw if nqp::isgt_i($any,1);
  1234. $any
  1235. }
  1236. method !ensure-limit-sanity(\limit --> Nil) {
  1237. X::TypeCheck.new(
  1238. operation => 'split ($limit argument)',
  1239. expected => 'any Real type (non-NaN) or Whatever',
  1240. got => limit.perl,
  1241. ).throw if limit === NaN;
  1242. limit = Inf if nqp::istype(limit,Whatever);
  1243. }
  1244. method parse-base(Str:D: Int:D $radix) {
  1245. fail X::Syntax::Number::RadixOutOfRange.new(:$radix)
  1246. unless 2 <= $radix <= 36; # (0..9,"a".."z").elems == 36
  1247. # do not modify $!value directly as that affects other same strings
  1248. my ($value, $sign, $sign-offset) = $!value, 1, 0;
  1249. given $value.substr(0,1) {
  1250. when '-'|'−' { $sign = -1; $sign-offset = 1 }
  1251. when '+' { $sign-offset = 1 }
  1252. }
  1253. if $value.contains('.') { # fractional
  1254. my ($whole, $fract) = $value.split: '.', 2;
  1255. my $w-parsed := nqp::radix_I($radix, $whole, $sign-offset, 0, Int);
  1256. my $f-parsed := nqp::radix_I($radix, $fract, 0, 0, Int);
  1257. # Whole part did not parse in its entirety
  1258. fail X::Str::Numeric.new(
  1259. :source($value),
  1260. :pos($w-parsed[2] max $sign-offset),
  1261. :reason("malformed base-$radix number"),
  1262. ) unless $w-parsed[2] == nqp::chars($whole)
  1263. or nqp::chars($whole) == $sign-offset; # or have no whole part
  1264. # Fractional part did not parse in its entirety
  1265. fail X::Str::Numeric.new(
  1266. :source($value),
  1267. :pos(
  1268. ($w-parsed[2] max $sign-offset)
  1269. + 1 # decimal dot
  1270. + ($f-parsed[2] max 0)
  1271. ),
  1272. :reason("malformed base-$radix number"),
  1273. ) unless $f-parsed[2] == nqp::chars($fract);
  1274. $sign * ($w-parsed[0] + $f-parsed[0]/$f-parsed[1]);
  1275. }
  1276. else { # Int
  1277. my $parsed := nqp::radix_I($radix, $value, $sign-offset, 0, Int);
  1278. # Did not parse the number in its entirety
  1279. fail X::Str::Numeric.new(
  1280. :source($value),
  1281. :pos($parsed[2] max $sign-offset),
  1282. :reason("malformed base-$radix number"),
  1283. ) unless $parsed[2] == nqp::chars($value);
  1284. $sign * $parsed[0];
  1285. }
  1286. }
  1287. method parse-names(Str:D:) {
  1288. my \names := nqp::split(',', self);
  1289. my int $elems = nqp::elems(names);
  1290. my int $i = -1;
  1291. my str $res = '';
  1292. nqp::while(
  1293. nqp::islt_i( ($i = nqp::add_i($i,1)), $elems ),
  1294. ($res = nqp::concat($res,
  1295. nqp::unless(
  1296. nqp::getstrfromname(nqp::atpos(names, $i).trim),
  1297. X::Str::InvalidCharName.new(
  1298. :name(nqp::atpos(names, $i).trim)
  1299. ).fail
  1300. ))),
  1301. );
  1302. $res
  1303. }
  1304. multi method split(Str:D: Regex:D $pat, $limit is copy = Inf;;
  1305. :$v is copy, :$k, :$kv, :$p, :$skip-empty) {
  1306. my int $any = self!ensure-split-sanity($v,$k,$kv,$p);
  1307. self!ensure-limit-sanity($limit);
  1308. return Seq.new(Rakudo::Iterator.Empty) if $limit <= 0;
  1309. my \matches = $limit == Inf
  1310. ?? self.match($pat, :g)
  1311. !! self.match($pat, :x(1..$limit-1));
  1312. my str $str = nqp::unbox_s(self);
  1313. my int $elems = +matches; # make sure all reified
  1314. return Seq.new(Rakudo::Iterator.OneValue(self)) unless $elems;
  1315. my $matches := nqp::getattr(matches,List,'$!reified');
  1316. my $result := nqp::create(IterationBuffer);
  1317. my int $i = -1;
  1318. my int $pos;
  1319. my int $found;
  1320. if $any || $skip-empty {
  1321. my int $notskip = !$skip-empty;
  1322. my int $next;
  1323. while nqp::islt_i(++$i,$elems) {
  1324. my $match := nqp::decont(nqp::atpos($matches,$i));
  1325. $found = nqp::getattr_i($match,Match,'$!from');
  1326. $next = $match.to;
  1327. if $notskip {
  1328. nqp::push($result,
  1329. nqp::substr($str,$pos,nqp::sub_i($found,$pos)));
  1330. }
  1331. elsif nqp::sub_i($found,$pos) -> $chars {
  1332. nqp::push($result,
  1333. nqp::substr($str,$pos,$chars));
  1334. }
  1335. nqp::if(
  1336. $any,
  1337. nqp::if(
  1338. $v,
  1339. nqp::push($result,$match), # v
  1340. nqp::if(
  1341. $k,
  1342. nqp::push($result,0), # k
  1343. nqp::if(
  1344. $kv,
  1345. nqp::stmts(
  1346. nqp::push($result,0), # kv
  1347. nqp::push($result,$match) # kv
  1348. ),
  1349. nqp::push($result, Pair.new(0,$match)) # $p
  1350. )
  1351. )
  1352. )
  1353. );
  1354. $pos = $next;
  1355. }
  1356. nqp::push($result,nqp::substr($str,$pos))
  1357. if $notskip || nqp::islt_i($pos,nqp::chars($str));
  1358. }
  1359. else {
  1360. my $match;
  1361. nqp::setelems($result,$elems + 1);
  1362. while nqp::islt_i(++$i,$elems) {
  1363. $match := nqp::decont(nqp::atpos($matches,$i));
  1364. $found = nqp::getattr_i($match,Match,'$!from');
  1365. nqp::bindpos($result,$i,
  1366. nqp::substr($str,$pos,nqp::sub_i($found,$pos)));
  1367. $pos = $match.to;
  1368. }
  1369. nqp::bindpos($result,$i,nqp::substr($str,$pos));
  1370. }
  1371. Seq.new(Rakudo::Iterator.ReifiedList($result))
  1372. }
  1373. multi method split(Str:D: Str(Cool) $match;;
  1374. :$v is copy, :$k, :$kv, :$p, :$skip-empty) {
  1375. my int $any = self!ensure-split-sanity($v,$k,$kv,$p);
  1376. # nothing to work with
  1377. my str $needle = nqp::unbox_s($match);
  1378. my int $chars = nqp::chars($needle);
  1379. return Seq.new($chars && !$skip-empty
  1380. ?? Rakudo::Iterator.OneValue(self)
  1381. !! Rakudo::Iterator.Empty
  1382. ) unless self.chars;
  1383. # split really, really fast in NQP, also supports ""
  1384. my $matches := nqp::split($needle,nqp::unbox_s(self));
  1385. # interleave the necessary strings if needed
  1386. if $chars {
  1387. if $any {
  1388. my $match-list :=
  1389. $v ?? nqp::list($needle)
  1390. !! $k ?? nqp::list(0)
  1391. !! $kv ?? nqp::list(0,$needle)
  1392. !! nqp::list(Pair.new(0,$needle)); # $p
  1393. if $match-list {
  1394. my int $i = nqp::elems($matches);
  1395. if $skip-empty {
  1396. nqp::splice($matches,$match-list,$i,
  1397. nqp::not_i(nqp::isne_i(
  1398. nqp::chars(nqp::atpos($matches,$i)),0)))
  1399. while $i = nqp::sub_i($i,1);
  1400. nqp::splice($matches,$empty,0,1)
  1401. unless nqp::chars(nqp::atpos($matches,0));
  1402. }
  1403. else {
  1404. nqp::splice($matches,$match-list,$i,0)
  1405. while $i = nqp::sub_i($i,1);
  1406. }
  1407. }
  1408. }
  1409. elsif $skip-empty {
  1410. my int $i = nqp::elems($matches);
  1411. my $match-list := nqp::list;
  1412. while nqp::isge_i($i = nqp::sub_i($i,1),0) {
  1413. nqp::splice($matches,$match-list,$i,1)
  1414. if nqp::iseq_i(nqp::chars(nqp::atpos($matches,$i)),0);
  1415. }
  1416. }
  1417. }
  1418. # single chars need empty before/after, unless inhibited
  1419. elsif !$skip-empty {
  1420. nqp::unshift($matches,"");
  1421. nqp::push($matches,"");
  1422. }
  1423. Seq.new(Rakudo::Iterator.ReifiedList($matches))
  1424. }
  1425. multi method split(Str:D: Str(Cool) $match, $limit is copy = Inf;;
  1426. :$v is copy, :$k, :$kv, :$p, :$skip-empty) {
  1427. my int $any = self!ensure-split-sanity($v,$k,$kv,$p);
  1428. self!ensure-limit-sanity($limit);
  1429. return Seq.new(Rakudo::Iterator.Empty) if $limit <= 0;
  1430. # nothing to work with
  1431. my int $chars = $match.chars;
  1432. if !self.chars {
  1433. return $chars ?? self.list !! ();
  1434. }
  1435. # nothing to do
  1436. elsif $limit == 1 {
  1437. return self.list;
  1438. }
  1439. # want them all
  1440. elsif $limit == Inf {
  1441. return self.split($match,:$v,:$k,:$kv,:$p,:$skip-empty);
  1442. }
  1443. # we have something to split on
  1444. elsif $chars {
  1445. # let the multi-needle handler handle all nameds
  1446. return self.split(($match,),$limit,:$v,:$k,:$kv,:$p,:$skip-empty)
  1447. if $any || $skip-empty;
  1448. # make the sequence
  1449. Seq.new(class :: does Iterator {
  1450. has str $!string;
  1451. has str $!chars;
  1452. has str $!match;
  1453. has int $!match-chars;
  1454. has int $!todo;
  1455. has int $!pos;
  1456. method !SET-SELF(\string, \match, \todo) {
  1457. $!string = nqp::unbox_s(string);
  1458. $!chars = nqp::chars($!string);
  1459. $!match = nqp::unbox_s(match);
  1460. $!match-chars = nqp::chars($!match);
  1461. $!todo = todo - 1;
  1462. self
  1463. }
  1464. method new(\string,\match,\todo) {
  1465. nqp::create(self)!SET-SELF(string,match,todo)
  1466. }
  1467. method !last-part() is raw {
  1468. my str $string = nqp::substr($!string,$!pos);
  1469. $!pos = $!chars + 1;
  1470. $!todo = 0;
  1471. nqp::p6box_s($string)
  1472. }
  1473. method !next-part(int $found) is raw {
  1474. my str $string =
  1475. nqp::substr($!string,$!pos, $found - $!pos);
  1476. $!pos = $found + $!match-chars;
  1477. nqp::p6box_s($string);
  1478. }
  1479. method pull-one() is raw {
  1480. if $!todo {
  1481. $!todo = $!todo - 1;
  1482. my int $found = nqp::index($!string,$!match,$!pos);
  1483. nqp::islt_i($found,0)
  1484. ?? nqp::isle_i($!pos,$!chars)
  1485. ?? self!last-part
  1486. !! IterationEnd
  1487. !! self!next-part($found);
  1488. }
  1489. else {
  1490. nqp::isle_i($!pos,$!chars)
  1491. ?? self!last-part
  1492. !! IterationEnd
  1493. }
  1494. }
  1495. method push-all($target --> IterationEnd) {
  1496. while $!todo {
  1497. $!todo = $!todo - 1;
  1498. my int $found = nqp::index($!string,$!match,$!pos);
  1499. nqp::islt_i($found,0)
  1500. ?? ($!todo = 0)
  1501. !! $target.push(self!next-part($found));
  1502. }
  1503. $target.push(self!last-part) if nqp::isle_i($!pos,$!chars);
  1504. }
  1505. method sink-all(--> IterationEnd) { }
  1506. }.new(self,$match,$limit));
  1507. }
  1508. # just separate chars
  1509. else {
  1510. Seq.new(class :: does Iterator {
  1511. has str $!string;
  1512. has int $!todo;
  1513. has int $!chars;
  1514. has int $!pos;
  1515. has int $!first;
  1516. has int $!last;
  1517. method !SET-SELF(\string, \todo, \skip-empty) {
  1518. $!string = nqp::unbox_s(string);
  1519. $!chars = nqp::chars($!string);
  1520. $!todo = todo;
  1521. $!first = !skip-empty;
  1522. if $!todo > $!chars + 2 { # will return all chars
  1523. $!todo = $!chars + 1;
  1524. $!last = !skip-empty;
  1525. }
  1526. else {
  1527. $!todo = $!todo - 1;
  1528. $!last = !skip-empty && ($!todo == $!chars + 1);
  1529. }
  1530. self
  1531. }
  1532. method new(\string,\todo,\skip-empty) {
  1533. nqp::create(self)!SET-SELF(string,todo,skip-empty)
  1534. }
  1535. method pull-one() is raw {
  1536. if $!first { # do empty string first
  1537. $!first = 0;
  1538. $!todo = $!todo - 1;
  1539. ""
  1540. }
  1541. elsif $!todo { # next char
  1542. $!todo = $!todo - 1;
  1543. nqp::p6box_s(nqp::substr($!string,$!pos++,1))
  1544. }
  1545. elsif $!last { # do final empty string
  1546. $!last = 0;
  1547. ""
  1548. }
  1549. elsif nqp::islt_i($!pos,$!chars) { # do rest of string
  1550. my str $rest = nqp::substr($!string,$!pos);
  1551. $!pos = $!chars;
  1552. nqp::p6box_s($rest)
  1553. }
  1554. else {
  1555. IterationEnd
  1556. }
  1557. }
  1558. method push-all($target --> IterationEnd) {
  1559. $target.push("") if $!first;
  1560. $!todo = $!todo - 1;
  1561. while $!todo {
  1562. $target.push(
  1563. nqp::p6box_s(nqp::substr($!string,$!pos++,1)));
  1564. $!todo = $!todo - 1;
  1565. }
  1566. $target.push( nqp::p6box_s(nqp::substr($!string,$!pos)))
  1567. if nqp::islt_i($!pos,$!chars);
  1568. $target.push("") if $!last;
  1569. }
  1570. method count-only() { nqp::p6box_i($!todo + $!first + $!last) }
  1571. method bool-only() { nqp::p6bool($!todo + $!first + $!last) }
  1572. method sink-all(--> IterationEnd) { }
  1573. }.new(self,$limit,$skip-empty));
  1574. }
  1575. }
  1576. multi method split(Str:D: @needles, $parts is copy = Inf;;
  1577. :$v is copy, :$k, :$kv, :$p, :$skip-empty) {
  1578. my int $any = self!ensure-split-sanity($v,$k,$kv,$p);
  1579. # must all be Cool, otherwise we'll just use a regex
  1580. return self.split(rx/ @needles /,:$v,:$k,:$kv,:$p,:$skip-empty) # / hl
  1581. unless Rakudo::Internals.ALL_TYPE(@needles,Cool);
  1582. self!ensure-limit-sanity($parts);
  1583. return Seq.new(Rakudo::Iterator.Empty) if $parts <= 0;
  1584. my int $limit = $parts.Int
  1585. unless nqp::istype($parts,Whatever) || $parts == Inf;
  1586. my str $str = nqp::unbox_s(self);
  1587. my $positions := nqp::list;
  1588. my $needles := nqp::list_s;
  1589. my $needle-chars := nqp::list_i;
  1590. my $needles-seen := nqp::hash;
  1591. my int $tried;
  1592. my int $fired;
  1593. # search using all needles
  1594. for @needles.kv -> int $index, $needle {
  1595. my str $need = nqp::unbox_s($needle.DEFINITE ?? $needle.Str !! "");
  1596. my int $chars = nqp::chars($need);
  1597. nqp::push_s($needles,$need);
  1598. nqp::push_i($needle-chars,$chars);
  1599. # search for this needle if there is one, and not done before
  1600. nqp::if(
  1601. nqp::isgt_i($chars,0)
  1602. && nqp::not_i(nqp::existskey($needles-seen,$need)),
  1603. nqp::stmts(
  1604. nqp::bindkey($needles-seen,$need,1),
  1605. (my int $pos),
  1606. (my int $i),
  1607. (my int $seen = nqp::elems($positions)),
  1608. nqp::if(
  1609. nqp::isgt_i($limit,0), # 0 = no limit
  1610. nqp::stmts(
  1611. (my int $todo = $limit),
  1612. nqp::while(
  1613. nqp::isge_i(($todo = nqp::sub_i($todo,1)),0)
  1614. && nqp::isge_i($i = nqp::index($str,$need,$pos),0),
  1615. nqp::stmts(
  1616. nqp::push($positions,nqp::list_i($i,$index)),
  1617. ($pos = nqp::add_i($i,1)),
  1618. )
  1619. )
  1620. ),
  1621. nqp::while(
  1622. nqp::isge_i($i = nqp::index($str,$need,$pos),0),
  1623. nqp::stmts(
  1624. nqp::push($positions,nqp::list_i($i,$index)),
  1625. ($pos = nqp::add_i($i,1))
  1626. )
  1627. )
  1628. ),
  1629. ($tried = nqp::add_i($tried,1)),
  1630. ($fired =
  1631. nqp::add_i($fired,nqp::isge_i(nqp::elems($positions),$seen)))
  1632. )
  1633. )
  1634. }
  1635. # no needle tried, assume we want chars
  1636. return self.split("",$limit) if nqp::not_i($tried);
  1637. # sort by position if more than one needle fired
  1638. $positions := nqp::getattr(
  1639. Rakudo::Sorting.MERGESORT-REIFIED-LIST-WITH(
  1640. nqp::p6bindattrinvres(
  1641. nqp::create(List),List,'$!reified',$positions
  1642. ),
  1643. -> \a, \b {
  1644. nqp::cmp_i(
  1645. nqp::atpos_i(a,0),
  1646. nqp::atpos_i(b,0)
  1647. ) || nqp::cmp_i(
  1648. nqp::atpos_i($needle-chars,nqp::atpos_i(b,1)),
  1649. nqp::atpos_i($needle-chars,nqp::atpos_i(a,1))
  1650. )
  1651. }
  1652. ),
  1653. List,
  1654. '$!reified'
  1655. ) if nqp::isgt_i($fired,1);
  1656. # remove elements we don't want
  1657. if nqp::isgt_i($limit,0) {
  1658. nqp::stmts(
  1659. (my int $limited = 1), # split one less than entries returned
  1660. (my int $elems = nqp::elems($positions)),
  1661. (my int $pos),
  1662. (my int $i = -1),
  1663. nqp::while(
  1664. nqp::islt_i(($i = nqp::add_i($i,1)),$elems)
  1665. && nqp::islt_i($limited,$limit),
  1666. nqp::if(
  1667. nqp::isge_i( # not hidden by other needle
  1668. nqp::atpos_i(nqp::atpos($positions,$i),0),
  1669. $pos
  1670. ),
  1671. nqp::stmts(
  1672. ($limited = nqp::add_i($limited,1)),
  1673. ($pos = nqp::add_i(
  1674. nqp::atpos_i(nqp::atpos($positions,$i),0),
  1675. nqp::atpos_i($needle-chars,
  1676. nqp::atpos_i(nqp::atpos($positions,$i),1))
  1677. ))
  1678. )
  1679. )
  1680. ),
  1681. nqp::if(
  1682. nqp::islt_i($i,$elems),
  1683. nqp::splice($positions,$empty,
  1684. $i,nqp::sub_i(nqp::elems($positions),$i))
  1685. )
  1686. )
  1687. }
  1688. # create the final result
  1689. my int $skip = ?$skip-empty;
  1690. my int $pos = 0;
  1691. my $result := nqp::create(IterationBuffer);
  1692. if $any {
  1693. nqp::stmts(
  1694. (my int $i = -1),
  1695. (my int $elems = nqp::elems($positions)),
  1696. nqp::while(
  1697. nqp::islt_i(($i = nqp::add_i($i,1)),$elems),
  1698. nqp::if(
  1699. nqp::isge_i( # not hidden by other needle
  1700. (my int $from = nqp::atpos_i(
  1701. (my $pair := nqp::atpos($positions,$i)),0)
  1702. ),
  1703. $pos
  1704. ),
  1705. nqp::stmts(
  1706. (my int $needle-index = nqp::atpos_i($pair,1)),
  1707. nqp::unless(
  1708. $skip && nqp::iseq_i($from,$pos),
  1709. nqp::push($result,
  1710. nqp::substr($str,$pos,nqp::sub_i($from,$pos)))
  1711. ),
  1712. nqp::if($k || $kv,
  1713. nqp::push($result,nqp::clone($needle-index))
  1714. ),
  1715. nqp::if($v || $kv,
  1716. nqp::push($result,nqp::atpos_s($needles,$needle-index))
  1717. ),
  1718. nqp::if($p,
  1719. nqp::push($result,Pair.new(
  1720. $needle-index,nqp::atpos_s($needles,$needle-index)))
  1721. ),
  1722. ($pos = nqp::add_i(
  1723. $from,
  1724. nqp::atpos_i($needle-chars,$needle-index)
  1725. ))
  1726. )
  1727. )
  1728. )
  1729. )
  1730. }
  1731. else {
  1732. nqp::stmts(
  1733. (my int $i = -1),
  1734. (my int $elems = nqp::elems($positions)),
  1735. nqp::while(
  1736. nqp::islt_i(($i = nqp::add_i($i,1)),$elems),
  1737. nqp::if(
  1738. nqp::isge_i( # not hidden by other needle
  1739. (my int $from = nqp::atpos_i(
  1740. (my $pair := nqp::atpos($positions,$i)),0)
  1741. ),
  1742. $pos
  1743. ),
  1744. nqp::stmts(
  1745. nqp::unless(
  1746. $skip && nqp::iseq_i($from,$pos),
  1747. nqp::push($result,
  1748. nqp::substr($str,$pos,nqp::sub_i($from,$pos))),
  1749. ),
  1750. ($pos = nqp::add_i($from,
  1751. nqp::atpos_i($needle-chars,nqp::atpos_i($pair,1))
  1752. ))
  1753. )
  1754. )
  1755. )
  1756. )
  1757. }
  1758. nqp::push($result,nqp::substr($str,$pos))
  1759. unless $skip && nqp::iseq_i($pos,nqp::chars($str));
  1760. Seq.new(Rakudo::Iterator.ReifiedList($result))
  1761. }
  1762. # Note that in these same* methods, as used by s/LHS/RHS/, the
  1763. # pattern is actually the original string matched by LHS, while the
  1764. # invocant "original" is really the replacement RHS part. Confusing...
  1765. method samecase(Str:D: Str:D $pattern) {
  1766. nqp::if(
  1767. nqp::chars(nqp::unbox_s($pattern)), # something to work with
  1768. nqp::stmts(
  1769. (my $result := nqp::list_s),
  1770. (my $cases := nqp::getattr($pattern,Str,'$!value')),
  1771. (my int $base-chars = nqp::chars($!value)),
  1772. (my int $cases-chars = nqp::if(
  1773. nqp::isgt_i(nqp::chars($cases),$base-chars),
  1774. $base-chars,
  1775. nqp::chars($cases)
  1776. )),
  1777. (my int $i = 0),
  1778. (my int $j = 0),
  1779. (my int $prev-case = nqp::if( # set up initial case
  1780. nqp::iscclass(nqp::const::CCLASS_LOWERCASE,$cases,0),
  1781. -1,
  1782. nqp::iscclass(nqp::const::CCLASS_UPPERCASE,$cases,0)
  1783. )),
  1784. nqp::while( # other chars in pattern
  1785. nqp::islt_i(($i = nqp::add_i($i,1)),$cases-chars),
  1786. nqp::stmts(
  1787. (my int $case = nqp::if( # -1 =lc, 1 = uc, 0 = else
  1788. nqp::iscclass(nqp::const::CCLASS_LOWERCASE,$cases,$i),
  1789. -1,
  1790. nqp::iscclass(nqp::const::CCLASS_UPPERCASE,$cases,$i)
  1791. )),
  1792. nqp::if(
  1793. nqp::isne_i($case,$prev-case),
  1794. nqp::stmts( # seen a change
  1795. nqp::push_s($result,nqp::if(
  1796. nqp::iseq_i($prev-case,-1), # coming from lc
  1797. nqp::lc(nqp::substr($!value,$j,nqp::sub_i($i,$j))),
  1798. nqp::if(
  1799. nqp::iseq_i($prev-case,1), # coming from uc
  1800. nqp::uc(nqp::substr($!value,$j,nqp::sub_i($i,$j))),
  1801. nqp::substr($!value,$j,nqp::sub_i($i,$j))
  1802. )
  1803. )),
  1804. ($prev-case = $case),
  1805. ($j = $i)
  1806. )
  1807. )
  1808. )
  1809. ),
  1810. nqp::if( # something left
  1811. nqp::islt_i($j,$base-chars),
  1812. nqp::push_s($result,nqp::if(
  1813. nqp::iseq_i($prev-case,-1), # must become lc
  1814. nqp::lc(nqp::substr($!value,$j,nqp::sub_i($base-chars,$j))),
  1815. nqp::if(
  1816. nqp::iseq_i($prev-case,1), # must become uc
  1817. nqp::uc(nqp::substr($!value,$j,nqp::sub_i($base-chars,$j))),
  1818. nqp::substr($!value,$j,nqp::sub_i($base-chars,$j))
  1819. )
  1820. ))
  1821. ),
  1822. nqp::join("",$result) # wrap it up
  1823. ),
  1824. self # nothing to be done
  1825. )
  1826. }
  1827. method samemark(Str:D: Str:D $pattern) {
  1828. nqp::if(
  1829. nqp::chars(nqp::unbox_s($pattern)), # something to work with
  1830. nqp::stmts(
  1831. (my $base := nqp::split("",$!value)),
  1832. (my $marks := nqp::split("",nqp::unbox_s($pattern))),
  1833. (my int $base-elems = nqp::elems($base)),
  1834. (my int $marks-elems = nqp::elems($marks) min $base-elems),
  1835. (my $result := nqp::setelems(nqp::list_s,$base-elems)),
  1836. (my int $i = -1),
  1837. nqp::while( # for all marks
  1838. nqp::islt_i(($i = nqp::add_i($i,1)),$marks-elems),
  1839. nqp::bindpos_s($result,$i, # store the result of:
  1840. nqp::stmts(
  1841. (my $marks-nfd := nqp::strtocodes( # char + accents of mark
  1842. nqp::atpos($marks,$i),
  1843. nqp::const::NORMALIZE_NFD,
  1844. nqp::create(NFD)
  1845. )),
  1846. nqp::shift_i($marks-nfd), # lose the char
  1847. (my $marks-base := nqp::strtocodes( # char + accents of base
  1848. nqp::atpos($base,$i),
  1849. nqp::const::NORMALIZE_NFD,
  1850. nqp::create(NFD)
  1851. )),
  1852. nqp::strfromcodes( # join base+rest of marks
  1853. nqp::splice(
  1854. $marks-base,
  1855. $marks-nfd,
  1856. 1,
  1857. nqp::sub_i(nqp::elems($marks-base),1)
  1858. )
  1859. )
  1860. )
  1861. )
  1862. ),
  1863. ($i = nqp::sub_i($i,1)),
  1864. nqp::while( # remaining base chars
  1865. nqp::islt_i(($i = nqp::add_i($i,1)),$base-elems),
  1866. nqp::bindpos_s($result,$i, # store the result of:
  1867. nqp::stmts(
  1868. ($marks-base := nqp::strtocodes( # char+all accents of base
  1869. nqp::atpos($base,$i),
  1870. nqp::const::NORMALIZE_NFD,
  1871. nqp::create(NFD)
  1872. )),
  1873. nqp::strfromcodes( # join base+rest of marks
  1874. nqp::splice(
  1875. $marks-base,
  1876. $marks-nfd, # NOTE: state of last iteration previous loop
  1877. 1,
  1878. nqp::sub_i(nqp::elems($marks-base),1)
  1879. )
  1880. )
  1881. )
  1882. )
  1883. ),
  1884. nqp::join("",$result) # wrap it up
  1885. ),
  1886. self # nothing to be done
  1887. )
  1888. }
  1889. method samespace(Str:D: Str:D $pattern) { self.word-by-word($pattern, :samespace) }
  1890. method word-by-word(Str:D: Str:D $pattern, &filter?, Bool :$samespace) {
  1891. my str $str = nqp::unbox_s(self);
  1892. my str $pat = nqp::unbox_s($pattern);
  1893. my Mu $ret := nqp::list_s;
  1894. my int $chars = nqp::chars($str);
  1895. my int $pos = 0;
  1896. my int $nextpos;
  1897. my int $patchars = nqp::chars($pat);
  1898. my int $patpos = 0;
  1899. my int $patnextpos;
  1900. my int $left;
  1901. my $patword;
  1902. # Still something to look for?
  1903. while ($left = $chars - $pos) > 0 {
  1904. $nextpos = nqp::findcclass(
  1905. nqp::const::CCLASS_WHITESPACE, $str, $pos, $left);
  1906. $patnextpos = nqp::findcclass(nqp::const::CCLASS_WHITESPACE, $pat, $patpos, $patchars - $patpos);
  1907. if &filter {
  1908. # We latch on last pattern word if pattern runs out of words first.
  1909. $patword := nqp::p6box_s(nqp::substr($pat, $patpos, $patnextpos - $patpos)) if $patpos < $patchars;
  1910. nqp::push_s($ret, nqp::unbox_s(filter(nqp::substr($str, $pos, $nextpos - $pos), $patword)));
  1911. }
  1912. else {
  1913. nqp::push_s($ret, nqp::substr($str, $pos, $nextpos - $pos));
  1914. }
  1915. # Did we have the last word?
  1916. last if $nextpos >= $chars;
  1917. $pos = nqp::findnotcclass( nqp::const::CCLASS_WHITESPACE,
  1918. $str, $nextpos, $chars - $nextpos);
  1919. if $patnextpos >= $patchars { # No more pat space, just copy original space.
  1920. nqp::push_s($ret,
  1921. nqp::substr($str, $nextpos, $pos - $nextpos));
  1922. $patpos = $patnextpos;
  1923. }
  1924. else { # Traverse pat space, use if wanted
  1925. $patpos = nqp::findnotcclass( nqp::const::CCLASS_WHITESPACE,
  1926. $pat, $patnextpos, $patchars - $patnextpos);
  1927. if $samespace { # Carry over pattern space?
  1928. nqp::push_s($ret,
  1929. nqp::substr($pat, $patnextpos, $patpos - $patnextpos));
  1930. }
  1931. else { # Nope, just use original space.
  1932. nqp::push_s($ret,
  1933. nqp::substr($str, $nextpos, $pos - $nextpos));
  1934. }
  1935. }
  1936. }
  1937. nqp::join("",$ret)
  1938. }
  1939. method trim-leading(Str:D:) {
  1940. my str $str = nqp::unbox_s(self);
  1941. my int $pos = nqp::findnotcclass(
  1942. nqp::const::CCLASS_WHITESPACE,
  1943. $str, 0, nqp::chars($str));
  1944. $pos ?? nqp::p6box_s(nqp::substr($str, $pos)) !! self;
  1945. }
  1946. method trim-trailing(Str:D:) {
  1947. my str $str = nqp::unbox_s(self);
  1948. my int $pos = nqp::chars($str) - 1;
  1949. $pos = $pos - 1
  1950. while nqp::isge_i($pos, 0)
  1951. && nqp::iscclass(nqp::const::CCLASS_WHITESPACE, $str, $pos);
  1952. nqp::islt_i($pos, 0) ?? '' !! nqp::p6box_s(nqp::substr($str, 0, $pos + 1));
  1953. }
  1954. method trim(Str:D:) {
  1955. my str $str = nqp::unbox_s(self);
  1956. my int $pos = nqp::chars($str) - 1;
  1957. my int $left = nqp::findnotcclass(
  1958. nqp::const::CCLASS_WHITESPACE, $str, 0, $pos + 1);
  1959. $pos = $pos - 1
  1960. while nqp::isge_i($pos, $left)
  1961. && nqp::iscclass(nqp::const::CCLASS_WHITESPACE, $str, $pos);
  1962. nqp::islt_i($pos, $left) ?? '' !! nqp::p6box_s(nqp::substr($str, $left, $pos + 1 - $left));
  1963. }
  1964. proto method words(|) { * }
  1965. multi method words(Str:D: :$autoderef!) { # in Actions.postprocess_words
  1966. my @list := self.words.List;
  1967. return @list == 1 ?? @list[0] !! @list;
  1968. }
  1969. multi method words(Str:D: $limit) {
  1970. nqp::istype($limit,Whatever) || $limit == Inf
  1971. ?? self.words
  1972. !! self.words.head($limit)
  1973. }
  1974. multi method words(Str:D:) {
  1975. Seq.new(class :: does Iterator {
  1976. has str $!str;
  1977. has int $!chars;
  1978. has int $!pos;
  1979. method !SET-SELF(\string) {
  1980. $!str = nqp::unbox_s(string);
  1981. $!chars = nqp::chars($!str);
  1982. $!pos = nqp::findnotcclass(
  1983. nqp::const::CCLASS_WHITESPACE, $!str, 0, $!chars);
  1984. self
  1985. }
  1986. method new(\string) { nqp::create(self)!SET-SELF(string) }
  1987. method pull-one() {
  1988. my int $left;
  1989. my int $nextpos;
  1990. if ($left = $!chars - $!pos) > 0 {
  1991. $nextpos = nqp::findcclass(
  1992. nqp::const::CCLASS_WHITESPACE, $!str, $!pos, $left);
  1993. my str $found =
  1994. nqp::substr($!str, $!pos, $nextpos - $!pos);
  1995. $!pos = nqp::findnotcclass( nqp::const::CCLASS_WHITESPACE,
  1996. $!str, $nextpos, $!chars - $nextpos);
  1997. return nqp::p6box_s($found);
  1998. }
  1999. IterationEnd
  2000. }
  2001. method push-all($target --> IterationEnd) {
  2002. my int $left;
  2003. my int $nextpos;
  2004. while ($left = $!chars - $!pos) > 0 {
  2005. $nextpos = nqp::findcclass(
  2006. nqp::const::CCLASS_WHITESPACE, $!str, $!pos, $left);
  2007. $target.push(nqp::p6box_s(
  2008. nqp::substr($!str, $!pos, $nextpos - $!pos)
  2009. ));
  2010. $!pos = nqp::findnotcclass( nqp::const::CCLASS_WHITESPACE,
  2011. $!str, $nextpos, $!chars - $nextpos);
  2012. }
  2013. }
  2014. }.new(self));
  2015. }
  2016. my $enc_type := nqp::hash('utf8',utf8,'utf16',utf16,'utf32',utf32);
  2017. my int $is-win = Rakudo::Internals.IS-WIN;
  2018. proto method encode(|) {*}
  2019. multi method encode(Str:D $encoding = 'utf8', Bool:D :$replacement) {
  2020. self.encode($encoding, :replacement($replacement
  2021. ?? ($encoding ~~ m:i/^utf/ ?? "\x[FFFD]" !! "?" )
  2022. !! Nil
  2023. ), |%_)
  2024. }
  2025. multi method encode(Str:D $encoding = 'utf8', Str :$replacement, Bool() :$translate-nl = False) {
  2026. my str $enc = Rakudo::Internals.NORMALIZE_ENCODING($encoding);
  2027. my $type := nqp::ifnull(nqp::atkey($enc_type,$enc),blob8);
  2028. my str $target = self;
  2029. if $is-win && $translate-nl {
  2030. $target .= subst("\n", "\r\n", :g);
  2031. }
  2032. return nqp::encoderep(nqp::unbox_s($target), $enc, nqp::unbox_s($replacement), nqp::decont($type.new))
  2033. if $replacement.defined;
  2034. nqp::encode(nqp::unbox_s($target), $enc, nqp::decont($type.new))
  2035. }
  2036. method NFC() {
  2037. nqp::strtocodes(nqp::unbox_s(self), nqp::const::NORMALIZE_NFC, nqp::create(NFC))
  2038. }
  2039. method NFD() {
  2040. nqp::strtocodes(nqp::unbox_s(self), nqp::const::NORMALIZE_NFD, nqp::create(NFD))
  2041. }
  2042. method NFKC() {
  2043. nqp::strtocodes(nqp::unbox_s(self), nqp::const::NORMALIZE_NFKC, nqp::create(NFKC))
  2044. }
  2045. method NFKD() {
  2046. nqp::strtocodes(nqp::unbox_s(self), nqp::const::NORMALIZE_NFKD, nqp::create(NFKD))
  2047. }
  2048. method wordcase(Str:D: :&filter = &tclc, Mu :$where = True) {
  2049. self.subst(:g, / [<:L> \w* ] +% <['\-]> /, -> $m { # ' highlighting
  2050. my Str $s = $m.Str;
  2051. $s ~~ $where ?? filter($s) !! $s;
  2052. });
  2053. }
  2054. proto method trans(|) { $/ := nqp::getlexcaller('$/'); {*} }
  2055. multi method trans(Str:D: Pair:D \what, *%n) {
  2056. my $from = what.key;
  2057. my $to = what.value;
  2058. $/ := nqp::getlexcaller('$/');
  2059. return self.trans((what,), |%n)
  2060. if !nqp::istype($from,Str) # from not a string
  2061. || !$from.defined # or a type object
  2062. || !nqp::istype($to,Str) # or to not a string
  2063. || !$to.defined # or a type object
  2064. || %n; # or any named params passed
  2065. # from 1 char
  2066. return Rakudo::Internals.TRANSPOSE(self, $from, substr($to,0,1))
  2067. if $from.chars == 1;
  2068. my str $sfrom = Rakudo::Internals.EXPAND-LITERAL-RANGE($from,0);
  2069. my str $str = nqp::unbox_s(self);
  2070. my str $chars = nqp::chars($str);
  2071. my Mu $result := nqp::list_s();
  2072. my str $check;
  2073. my int $i = -1;
  2074. # something to convert to
  2075. if $to.chars -> $tochars {
  2076. nqp::setelems($result,$chars);
  2077. # all convert to one char
  2078. if $tochars == 1 {
  2079. my str $sto = nqp::unbox_s($to);
  2080. while nqp::islt_i(++$i,$chars) {
  2081. $check = nqp::substr($str,$i,1);
  2082. nqp::bindpos_s(
  2083. $result, $i, nqp::iseq_i(nqp::index($sfrom,$check),-1)
  2084. ?? $check
  2085. !! $sto
  2086. );
  2087. }
  2088. }
  2089. # multiple chars to convert to
  2090. else {
  2091. my str $sto = Rakudo::Internals.EXPAND-LITERAL-RANGE($to,0);
  2092. my int $sfl = nqp::chars($sfrom);
  2093. my int $found;
  2094. # repeat until mapping complete
  2095. $sto = $sto ~ $sto while nqp::islt_i(nqp::chars($sto),$sfl);
  2096. while nqp::islt_i(++$i,$chars) {
  2097. $check = nqp::substr($str,$i,1);
  2098. $found = nqp::index($sfrom,$check);
  2099. nqp::bindpos_s($result, $i, nqp::iseq_i($found,-1)
  2100. ?? $check
  2101. !! nqp::substr($sto,$found,1)
  2102. );
  2103. }
  2104. }
  2105. }
  2106. # just remove
  2107. else {
  2108. while nqp::islt_i(++$i,$chars) {
  2109. $check = nqp::substr($str,$i,1);
  2110. nqp::push_s($result, $check)
  2111. if nqp::iseq_i(nqp::index($sfrom,$check),-1);
  2112. }
  2113. }
  2114. nqp::p6box_s(nqp::join('',$result));
  2115. }
  2116. my class LSM {
  2117. has str $!source;
  2118. has $!substitutions;
  2119. has int $!squash;
  2120. has int $!complement;
  2121. has str $!prev_result;
  2122. has int $!index;
  2123. has int $!next_match;
  2124. has int $!substitution_length;
  2125. has $!first_substitution; # need this one for :c with arrays
  2126. has $!next_substitution;
  2127. has $!match_obj;
  2128. has $!last_match_obj;
  2129. has str $!unsubstituted_text;
  2130. has str $!substituted_text;
  2131. method !SET-SELF(\source,\substitutions,\squash,\complement) {
  2132. $!source = nqp::unbox_s(source);
  2133. $!substitutions := nqp::getattr(substitutions,List,'$!reified');
  2134. $!squash = ?squash;
  2135. $!complement = ?complement;
  2136. $!prev_result = '';
  2137. self
  2138. }
  2139. method new(\source,\substitutions,\squash,\complement) {
  2140. nqp::create(self)!SET-SELF(source,substitutions,squash,complement)
  2141. }
  2142. method !compare_substitution(
  2143. $substitution, int $pos, int $length --> Nil
  2144. ) {
  2145. if nqp::isgt_i($!next_match,$pos)
  2146. || nqp::iseq_i($!next_match,$pos)
  2147. && nqp::islt_i($!substitution_length,$length) {
  2148. $!next_match = $pos;
  2149. $!substitution_length = $length;
  2150. $!next_substitution = $substitution;
  2151. $!match_obj = $!last_match_obj;
  2152. }
  2153. }
  2154. method !increment_index($s --> Nil) {
  2155. $/ := nqp::getlexcaller('$/');
  2156. if nqp::istype($s,Regex) {
  2157. $!index = $!next_match + (
  2158. substr($!source,$!index) ~~ $s ?? $/.chars !! 0
  2159. );
  2160. $!last_match_obj = $/;
  2161. }
  2162. else {
  2163. $!index = $!next_match
  2164. + nqp::chars(nqp::istype($s,Str) ?? $s !! $s.Str);
  2165. }
  2166. }
  2167. # note: changes outer $/
  2168. method get_next_substitution_result {
  2169. my $value = $!complement
  2170. ?? $!first_substitution.value
  2171. !! $!next_substitution.value;
  2172. my $outer_slash := nqp::getlexcaller('$/');
  2173. $/ := nqp::getlexcaller('$/');
  2174. $outer_slash = $!match_obj;
  2175. my str $result = nqp::istype($value,Callable)
  2176. ?? $value().Str
  2177. !! nqp::istype($value,Str)
  2178. ?? $value
  2179. !! $value.Str;
  2180. my str $orig_result = $result;
  2181. $result = ''
  2182. if $!squash
  2183. && nqp::chars($!prev_result)
  2184. && nqp::iseq_s($!prev_result,$result)
  2185. && nqp::iseq_s($!unsubstituted_text,'');
  2186. $!prev_result = $orig_result;
  2187. $result
  2188. }
  2189. method next_substitution() {
  2190. $/ := nqp::getlexcaller('$/');
  2191. $!next_match = nqp::chars($!source);
  2192. $!first_substitution = nqp::atpos($!substitutions,0)
  2193. unless nqp::defined($!first_substitution);
  2194. # triage substitutions left to do
  2195. my $todo := nqp::list;
  2196. my $iter := nqp::iterator($!substitutions);
  2197. while $iter {
  2198. my $this := nqp::shift($iter);
  2199. my $key := $this.key;
  2200. if nqp::istype($key,Regex) {
  2201. if $!source.match($key, :continue($!index)) -> \m {
  2202. $!last_match_obj = $/;
  2203. self!compare_substitution($this, m.from, m.to - m.from);
  2204. nqp::push($todo,$this);
  2205. }
  2206. }
  2207. elsif nqp::istype($key,Cool) {
  2208. my str $skey = nqp::istype($key,Str) ?? $key !! $key.Str;
  2209. my int $pos = nqp::index($!source,$skey,$!index);
  2210. if nqp::isge_i($pos,0) {
  2211. self!compare_substitution($this,$pos,nqp::chars($skey));
  2212. nqp::push($todo,$this);
  2213. }
  2214. }
  2215. else {
  2216. X::Str::Trans::IllegalKey.new(key => $this).throw;
  2217. }
  2218. }
  2219. $!substitutions := $todo;
  2220. $!unsubstituted_text =
  2221. nqp::substr($!source,$!index,$!next_match - $!index);
  2222. if $!next_substitution.defined {
  2223. if $!complement {
  2224. my $oldidx = $!index;
  2225. if nqp::chars($!unsubstituted_text) -> \todo {
  2226. my $result = self.get_next_substitution_result;
  2227. self!increment_index($!next_substitution.key);
  2228. $!substituted_text = nqp::substr(
  2229. $!source,
  2230. $oldidx + todo,
  2231. $!index - $oldidx - todo,
  2232. );
  2233. $!unsubstituted_text = $!squash
  2234. ?? $result
  2235. !! $result x todo;
  2236. }
  2237. else {
  2238. return if $!next_match == nqp::chars($!source);
  2239. my $result = self.get_next_substitution_result;
  2240. self!increment_index($!next_substitution.key);
  2241. $!substituted_text = '';
  2242. $!unsubstituted_text =
  2243. nqp::substr($!source,$oldidx,$!index - $oldidx);
  2244. }
  2245. }
  2246. else {
  2247. return if $!next_match == nqp::chars($!source);
  2248. $!substituted_text = self.get_next_substitution_result;
  2249. self!increment_index($!next_substitution.key);
  2250. }
  2251. }
  2252. nqp::islt_i($!next_match,nqp::chars($!source))
  2253. && nqp::elems($!substitutions)
  2254. }
  2255. method result() {
  2256. $/ := nqp::getlexcaller('$/');
  2257. my Mu $result := nqp::list_s;
  2258. while self.next_substitution {
  2259. nqp::push_s($result,$!unsubstituted_text);
  2260. nqp::push_s($result,$!substituted_text);
  2261. }
  2262. nqp::push_s($result,$!unsubstituted_text);
  2263. nqp::p6box_s(nqp::join('', $result))
  2264. }
  2265. }
  2266. multi method trans(Str:D:
  2267. *@changes, :c(:$complement), :s(:$squash), :d(:$delete)) {
  2268. # nothing to do
  2269. return self unless self.chars;
  2270. $/ := nqp::getlexcaller('$/');
  2271. my sub myflat(*@s) {
  2272. @s.map: { nqp::istype($_, Iterable) ?? .list.Slip !! $_ }
  2273. }
  2274. my sub expand($s) {
  2275. nqp::istype($s,Iterable) || nqp::istype($s,Positional)
  2276. ?? (my @ = myflat($s.list).Slip)
  2277. !! Rakudo::Internals.EXPAND-LITERAL-RANGE($s,1)
  2278. }
  2279. my int $just-strings = !$complement && !$squash;
  2280. my int $just-chars = $just-strings;
  2281. my $needles := nqp::list;
  2282. my $pins := nqp::list;
  2283. my $substitutions := nqp::list;
  2284. for @changes -> $p {
  2285. X::Str::Trans::InvalidArg.new(got => $p).throw
  2286. unless nqp::istype($p,Pair);
  2287. my $key := $p.key;
  2288. my $value := $p.value;
  2289. if nqp::istype($key,Regex) {
  2290. $just-strings = 0;
  2291. nqp::push($substitutions,$p);
  2292. }
  2293. elsif nqp::istype($value,Callable) {
  2294. $just-strings = 0;
  2295. nqp::push($substitutions,Pair.new($_,$value)) for expand $key;
  2296. }
  2297. else {
  2298. my $from := nqp::getattr(expand($key), List,'$!reified');
  2299. my $to := nqp::getattr(expand($value),List,'$!reified');
  2300. my $from-elems = nqp::elems($from);
  2301. my $to-elems = nqp::elems($to);
  2302. my $padding = $delete
  2303. ?? ''
  2304. !! $to-elems
  2305. ?? nqp::atpos($to,$to-elems - 1)
  2306. !! '';
  2307. my int $i = -1;
  2308. while nqp::islt_i($i = $i + 1,$from-elems) {
  2309. my $key := nqp::atpos($from,$i);
  2310. my $value := nqp::islt_i($i,$to-elems)
  2311. ?? nqp::atpos($to,$i)
  2312. !! $padding;
  2313. nqp::push($substitutions,Pair.new($key,$value));
  2314. if $just-strings {
  2315. if nqp::istype($key,Str) && nqp::istype($value,Str) {
  2316. $key := nqp::unbox_s($key);
  2317. $just-chars = 0 if nqp::isgt_i(nqp::chars($key),1);
  2318. nqp::push($needles,$key);
  2319. nqp::push($pins,nqp::unbox_s($value));
  2320. }
  2321. else {
  2322. $just-strings = 0;
  2323. }
  2324. }
  2325. }
  2326. }
  2327. }
  2328. # can do special cases for just strings
  2329. if $just-strings {
  2330. # only need to go through string once
  2331. if $just-chars {
  2332. my $lookup := nqp::hash;
  2333. my int $elems = nqp::elems($needles);
  2334. my int $i = -1;
  2335. nqp::bindkey($lookup,
  2336. nqp::atpos($needles,$i),nqp::atpos($pins,$i))
  2337. while nqp::islt_i($i = $i + 1,$elems);
  2338. my $result := nqp::split("",nqp::unbox_s(self));
  2339. $i = -1;
  2340. $elems = nqp::elems($result);
  2341. nqp::bindpos($result,$i,
  2342. nqp::atkey($lookup,nqp::atpos($result,$i)))
  2343. if nqp::existskey($lookup,nqp::atpos($result,$i))
  2344. while nqp::islt_i($i = $i + 1,$elems);
  2345. nqp::join("",$result)
  2346. }
  2347. # use multi-needle split with in-place mapping
  2348. else {
  2349. nqp::stmts(
  2350. (my $iterator := self.split($needles,:k).iterator),
  2351. (my $strings := nqp::list_s($iterator.pull-one)),
  2352. nqp::until(
  2353. nqp::eqaddr((my $i := $iterator.pull-one),IterationEnd),
  2354. nqp::stmts(
  2355. nqp::push_s($strings,nqp::atpos($pins,$i)),
  2356. nqp::push_s($strings,$iterator.pull-one)
  2357. )
  2358. ),
  2359. nqp::join("",$strings)
  2360. )
  2361. }
  2362. }
  2363. # alas, need to use more complex route
  2364. else {
  2365. LSM.new(self,$substitutions,$squash,$complement).result;
  2366. }
  2367. }
  2368. proto method indent($) {*}
  2369. # Zero indent does nothing
  2370. multi method indent(Int() $steps where { $_ == 0 }) {
  2371. self;
  2372. }
  2373. # Positive indent does indent
  2374. multi method indent(Int() $steps where { $_ > 0 }) {
  2375. # We want to keep trailing \n so we have to .comb explicitly instead of .lines
  2376. self.comb(/:r ^^ \N* \n?/).map({
  2377. given $_.Str {
  2378. when /^ \n? $ / {
  2379. $_;
  2380. }
  2381. # Use the existing space character if they're all the same
  2382. # (but tabs are done slightly differently)
  2383. when /^(\t+) ([ \S .* | $ ])/ {
  2384. $0 ~ "\t" x ($steps div $?TABSTOP) ~
  2385. ' ' x ($steps mod $?TABSTOP) ~ $1
  2386. }
  2387. when /^(\h) $0* [ \S | $ ]/ {
  2388. $0 x $steps ~ $_
  2389. }
  2390. # Otherwise we just insert spaces after the existing leading space
  2391. default {
  2392. $_ ~~ /^(\h*) (.*)$/;
  2393. $0 ~ (' ' x $steps) ~ $1
  2394. }
  2395. }
  2396. }).join;
  2397. }
  2398. # Negative indent (de-indent)
  2399. multi method indent(Int() $steps where { $_ < 0 }) {
  2400. de-indent(self, $steps);
  2401. }
  2402. # Whatever indent (de-indent)
  2403. multi method indent(Whatever $steps) {
  2404. de-indent(self, $steps);
  2405. }
  2406. sub de-indent($obj, $steps) {
  2407. # Loop through all lines to get as much info out of them as possible
  2408. my @lines = $obj.comb(/:r ^^ \N* \n?/).map({
  2409. # Split the line into indent and content
  2410. my ($indent, $rest) = @($_ ~~ /^(\h*) (.*)$/);
  2411. # Split the indent into characters and annotate them
  2412. # with their visual size
  2413. my $indent-size = 0;
  2414. my @indent-chars = $indent.comb.map(-> $char {
  2415. my $width = $char eq "\t"
  2416. ?? $?TABSTOP - ($indent-size mod $?TABSTOP)
  2417. !! 1;
  2418. $indent-size += $width;
  2419. $char => $width;
  2420. }).eager;
  2421. { :$indent-size, :@indent-chars, :rest(~$rest) };
  2422. });
  2423. # Figure out the amount * should de-indent by, we also use this for warnings
  2424. my $common-prefix = min @lines.grep({ .<indent-size> || .<rest> ~~ /\S/}).map({ $_<indent-size> });
  2425. return $obj if $common-prefix === Inf;
  2426. # Set the actual de-indent amount here
  2427. my Int $de-indent = nqp::istype($steps,Whatever)
  2428. ?? $common-prefix
  2429. !! -$steps;
  2430. warn "Asked to remove $de-indent spaces, but the shortest indent is $common-prefix spaces"
  2431. if $de-indent > $common-prefix;
  2432. # Work forwards from the left end of the indent whitespace, removing
  2433. # array elements up to # (or over, in the case of tab-explosion)
  2434. # the specified de-indent amount.
  2435. @lines.map(-> $l {
  2436. my $pos = 0;
  2437. while $l<indent-chars> and $pos < $de-indent {
  2438. if $l<indent-chars>.shift.key eq "\t" {
  2439. $pos -= $pos % $?TABSTOP;
  2440. $pos += $?TABSTOP;
  2441. } else {
  2442. $pos++
  2443. }
  2444. }
  2445. if $l<indent-chars> and $pos % $?TABSTOP {
  2446. my $check = $?TABSTOP - $pos % $?TABSTOP;
  2447. $check = $l<indent-chars>[lazy 0..^$check].first(*.key eq "\t",:k);
  2448. with $check {
  2449. $l<indent-chars>.shift for 0..$check;
  2450. $pos -= $pos % $?TABSTOP;
  2451. $pos += $?TABSTOP;
  2452. }
  2453. }
  2454. $l<indent-chars>».key.join ~ ' ' x ($pos - $de-indent) ~ $l<rest>;
  2455. }).join;
  2456. }
  2457. proto method codes(|) { * }
  2458. multi method codes(Str:D: --> Int:D) {
  2459. self.NFC.codes
  2460. }
  2461. multi method codes(Str:U: --> Int:D) {
  2462. self.Str; # generate undefined warning
  2463. 0
  2464. }
  2465. proto method chars(|) { * }
  2466. multi method chars(Str:D: --> Int:D) {
  2467. nqp::p6box_i(nqp::chars($!value))
  2468. }
  2469. multi method chars(Str:U: --> Int:D) {
  2470. self.Str; # generate undefined warning
  2471. 0
  2472. }
  2473. proto method uc(|) { * }
  2474. multi method uc(Str:D:) {
  2475. nqp::p6box_s(nqp::uc($!value));
  2476. }
  2477. multi method uc(Str:U:) {
  2478. self.Str;
  2479. }
  2480. proto method lc(|) { * }
  2481. multi method lc(Str:D:) {
  2482. nqp::p6box_s(nqp::lc($!value));
  2483. }
  2484. multi method lc(Str:U:) {
  2485. self.Str;
  2486. }
  2487. proto method tc(|) { * }
  2488. multi method tc(Str:D:) {
  2489. nqp::p6box_s(nqp::tc(nqp::substr($!value,0,1)) ~ nqp::substr($!value,1));
  2490. }
  2491. multi method tc(Str:U:) {
  2492. self.Str
  2493. }
  2494. proto method fc(|) { * }
  2495. multi method fc(Str:D:) {
  2496. nqp::p6box_s(nqp::fc($!value));
  2497. }
  2498. multi method fc(Str:U:) {
  2499. self.Str;
  2500. }
  2501. proto method tclc(|) { * }
  2502. multi method tclc(Str:D:) {
  2503. nqp::p6box_s(nqp::tclc($!value))
  2504. }
  2505. multi method tclc(Str:U:) {
  2506. self.Str
  2507. }
  2508. proto method flip(|) { * }
  2509. multi method flip(Str:D:) {
  2510. nqp::p6box_s(nqp::flip($!value))
  2511. }
  2512. multi method flip(Str:U:) {
  2513. self.Str
  2514. }
  2515. proto method ord(|) { * }
  2516. multi method ord(Str:D: --> Int:D) {
  2517. nqp::chars($!value)
  2518. ?? nqp::p6box_i(nqp::ord($!value))
  2519. !! Nil;
  2520. }
  2521. multi method ord(Str:U: --> Nil) { }
  2522. }
  2523. multi sub prefix:<~>(Str:D \a) { a.Str }
  2524. multi sub prefix:<~>(str $a --> str) { $a }
  2525. multi sub infix:<~>(Str:D \a, Str:D \b --> Str:D) {
  2526. nqp::p6box_s(nqp::concat(nqp::unbox_s(a), nqp::unbox_s(b)))
  2527. }
  2528. multi sub infix:<~>(str $a, str $b --> str) { nqp::concat($a, $b) }
  2529. multi sub infix:<~>(*@args --> Str:D) { @args.join }
  2530. multi sub infix:<x>(Str:D $s, Int:D $repetition --> Str:D) {
  2531. nqp::if(nqp::islt_i($repetition, 0),
  2532. '',
  2533. nqp::p6box_s(nqp::x(nqp::unbox_s($s), nqp::unbox_i($repetition))))
  2534. }
  2535. multi sub infix:<x>(str $s, int $repetition --> str) {
  2536. nqp::if(nqp::islt_i($repetition, 0), '', nqp::x($s, $repetition))
  2537. }
  2538. multi sub infix:<cmp>(Str:D \a, Str:D \b --> Order:D) {
  2539. ORDER(nqp::cmp_s(nqp::unbox_s(a), nqp::unbox_s(b)))
  2540. }
  2541. multi sub infix:<cmp>(str $a, str $b --> Order:D) {
  2542. ORDER(nqp::cmp_s($a, $b))
  2543. }
  2544. multi sub infix:<===>(Str:D \a, Str:D \b --> Bool:D) {
  2545. nqp::p6bool(
  2546. nqp::eqaddr(a.WHAT,b.WHAT)
  2547. && nqp::iseq_s(nqp::unbox_s(a), nqp::unbox_s(b))
  2548. )
  2549. }
  2550. multi sub infix:<===>(str $a, str $b --> Bool:D) {
  2551. nqp::p6bool(nqp::iseq_s($a, $b))
  2552. }
  2553. multi sub infix:<leg>(Str:D \a, Str:D \b --> Order:D) {
  2554. ORDER(nqp::cmp_s(nqp::unbox_s(a), nqp::unbox_s(b)))
  2555. }
  2556. multi sub infix:<leg>(str $a, str $b --> Order:D) {
  2557. ORDER(nqp::cmp_s($a, $b))
  2558. }
  2559. multi sub infix:<eq>(Str:D \a, Str:D \b --> Bool:D) {
  2560. nqp::p6bool(nqp::iseq_s(nqp::unbox_s(a), nqp::unbox_s(b)))
  2561. }
  2562. multi sub infix:<eq>(str $a, str $b --> Bool:D) {
  2563. nqp::p6bool(nqp::iseq_s($a, $b))
  2564. }
  2565. multi sub infix:<ne>(Str:D \a, Str:D \b --> Bool:D) {
  2566. nqp::p6bool(nqp::isne_s(nqp::unbox_s(a), nqp::unbox_s(b)))
  2567. }
  2568. multi sub infix:<ne>(str $a, str $b --> Bool:D) {
  2569. nqp::p6bool(nqp::isne_s($a, $b))
  2570. }
  2571. multi sub infix:<lt>(Str:D \a, Str:D \b --> Bool:D) {
  2572. nqp::p6bool(nqp::islt_s(nqp::unbox_s(a), nqp::unbox_s(b)))
  2573. }
  2574. multi sub infix:<lt>(str $a, str $b --> Bool:D) {
  2575. nqp::p6bool(nqp::islt_s($a, $b))
  2576. }
  2577. multi sub infix:<le>(Str:D \a, Str:D \b --> Bool:D) {
  2578. nqp::p6bool(nqp::isle_s(nqp::unbox_s(a), nqp::unbox_s(b)))
  2579. }
  2580. multi sub infix:<le>(str $a, str $b --> Bool:D) {
  2581. nqp::p6bool(nqp::isle_s($a, $b))
  2582. }
  2583. multi sub infix:<gt>(Str:D \a, Str:D \b --> Bool:D) {
  2584. nqp::p6bool(nqp::isgt_s(nqp::unbox_s(a), nqp::unbox_s(b)))
  2585. }
  2586. multi sub infix:<gt>(str $a, str $b --> Bool:D) {
  2587. nqp::p6bool(nqp::isgt_s($a, $b))
  2588. }
  2589. multi sub infix:<ge>(Str:D \a, Str:D \b --> Bool:D) {
  2590. nqp::p6bool(nqp::isge_s(nqp::unbox_s(a), nqp::unbox_s(b)))
  2591. }
  2592. multi sub infix:<le>(str $a, str $b --> Bool:D) {
  2593. nqp::p6bool(nqp::isle_s($a, $b))
  2594. }
  2595. multi sub infix:<~|>(Str:D \a, Str:D \b --> Str:D) {
  2596. nqp::p6box_s(nqp::bitor_s(nqp::unbox_s(a), nqp::unbox_s(b)))
  2597. }
  2598. multi sub infix:<~|>(str $a, str $b --> str) { nqp::bitor_s($a, $b) }
  2599. multi sub infix:<~&>(Str:D \a, Str:D \b --> Str:D) {
  2600. nqp::p6box_s(nqp::bitand_s(nqp::unbox_s(a), nqp::unbox_s(b)))
  2601. }
  2602. multi sub infix:<~&>(str $a, str $b --> str) { nqp::bitand_s($a, $b) }
  2603. multi sub infix:<~^>(Str:D \a, Str:D \b --> Str:D) {
  2604. nqp::p6box_s(nqp::bitxor_s(nqp::unbox_s(a), nqp::unbox_s(b)))
  2605. }
  2606. multi sub infix:<~^>(str $a, str $b --> str) { nqp::bitxor_s($a, $b) }
  2607. multi sub prefix:<~^>(Str \a) {
  2608. Failure.new("prefix:<~^> NYI") # XXX
  2609. }
  2610. # XXX: String-wise shifts NYI
  2611. multi sub infix:«~>»(Str:D \a, Int:D \b --> Str:D) {
  2612. X::NYI.new(feature => "infix:«~>»").throw;
  2613. }
  2614. multi sub infix:«~>»(str $a, int $b) {
  2615. X::NYI.new(feature => "infix:«~>»").throw;
  2616. }
  2617. multi sub infix:«~<»(Str:D \a, Int:D \b --> Str:D) {
  2618. X::NYI.new(feature => "infix:«~<»").throw;
  2619. }
  2620. multi sub infix:«~<»(str $a, int $b) {
  2621. X::NYI.new(feature => "infix:«~<»").throw;
  2622. }
  2623. multi sub ords(Str $s) {
  2624. $s.ords
  2625. }
  2626. # TODO: Cool variants
  2627. sub trim (Str:D $s --> Str:D) { $s.trim }
  2628. sub trim-leading (Str:D $s --> Str:D) { $s.trim-leading }
  2629. sub trim-trailing(Str:D $s --> Str:D) { $s.trim-trailing }
  2630. # the opposite of Real.base, used for :16($hex_str)
  2631. proto sub UNBASE (|) { * }
  2632. multi sub UNBASE(Int:D $base, Any:D $num) {
  2633. X::Numeric::Confused.new(:$num, :$base).throw;
  2634. }
  2635. multi sub UNBASE(Int:D $base, Str:D $str) {
  2636. my Str $ch = substr($str, 0, 1);
  2637. if $ch eq '0' {
  2638. $ch = substr($str, 1, 1);
  2639. if $base <= 11 && $ch eq any(<x d o b>)
  2640. or $base <= 24 && $ch eq any <o x>
  2641. or $base <= 33 && $ch eq 'x' {
  2642. $str.Numeric;
  2643. } else {
  2644. ":{$base}<$str>".Numeric;
  2645. }
  2646. } elsif $ch eq ':' && substr($str, 1, 1) ~~ ('1'..'9') {
  2647. $str.Numeric;
  2648. } else {
  2649. ":{$base}<$str>".Numeric;
  2650. }
  2651. }
  2652. # for :16[1, 2, 3]
  2653. sub UNBASE_BRACKET($base, @a) {
  2654. my $v = 0;
  2655. my $denom = 1;
  2656. my Bool $seen-dot = False;
  2657. for @a {
  2658. if $seen-dot {
  2659. die "Only one decimal dot allowed" if $_ eq '.';
  2660. $denom *= $base;
  2661. $v += $_ / $denom
  2662. }
  2663. elsif $_ eq '.' {
  2664. $seen-dot = True;
  2665. }
  2666. else {
  2667. $v = $v * $base + $_;
  2668. }
  2669. }
  2670. $v;
  2671. }
  2672. proto sub infix:<unicmp>(|) is pure { * }
  2673. proto sub infix:<coll>(|) { * }
  2674. multi sub infix:<unicmp>(Str:D \a, Str:D \b --> Order:D) {
  2675. nqp::isnull(nqp::getlexcaller('EXPERIMENTAL-COLLATION')) and X::Experimental.new(
  2676. feature => "the 'unicmp' operator",
  2677. use => "collation"
  2678. ).throw;
  2679. ORDER(
  2680. nqp::unicmp_s(
  2681. nqp::unbox_s(a), nqp::unbox_s(b), 15,0,0))
  2682. }
  2683. multi sub infix:<unicmp>(Pair:D \a, Pair:D \b) {
  2684. (a.key unicmp b.key) || (a.value unicmp b.value)
  2685. }
  2686. multi sub infix:<coll>(Str:D \a, Str:D \b --> Order:D) {
  2687. nqp::isnull(nqp::getlexcaller('EXPERIMENTAL-COLLATION')) and X::Experimental.new(
  2688. feature => "the 'coll' operator",
  2689. use => "collation"
  2690. ).throw;
  2691. ORDER(
  2692. nqp::unicmp_s(
  2693. nqp::unbox_s(a), nqp::unbox_s(b), $*COLLATION.collation-level,0,0))
  2694. }
  2695. multi sub infix:<coll>(Cool:D \a, Cool:D \b --> Order:D) {
  2696. nqp::isnull(nqp::getlexcaller('EXPERIMENTAL-COLLATION')) and X::Experimental.new(
  2697. feature => "the 'coll' operator",
  2698. use => "collation"
  2699. ).throw;
  2700. ORDER(
  2701. nqp::unicmp_s(
  2702. nqp::unbox_s(a.Str), nqp::unbox_s(b.Str), $*COLLATION.collation-level,0,0))
  2703. }
  2704. multi sub infix:<coll>(Pair:D \a, Pair:D \b) {
  2705. (a.key coll b.key) || (a.value coll b.value)
  2706. }
  2707. sub chrs(*@c --> Str:D) {
  2708. fail X::Cannot::Lazy.new(action => 'chrs') if @c.is-lazy;
  2709. my $list := nqp::getattr(@c,List,'$!reified');
  2710. my int $i = -1;
  2711. my int $elems = nqp::elems($list);
  2712. my $result := nqp::list_s;
  2713. nqp::setelems($result,$elems);
  2714. my $value;
  2715. nqp::istype(($value := nqp::atpos($list,$i)),Int)
  2716. ?? nqp::bindpos_s($result,$i,nqp::chr($value))
  2717. !! nqp::istype($value, Str)
  2718. ?? (nqp::istype(($value := +$value), Failure)
  2719. ?? return $value
  2720. !! nqp::bindpos_s($result,$i,nqp::chr($value)))
  2721. !! fail X::TypeCheck.new(
  2722. operation => "converting element #$i to .chr",
  2723. got => $value,
  2724. expected => Int)
  2725. while nqp::islt_i(++$i,$elems);
  2726. nqp::join("",$result)
  2727. }
  2728. proto sub parse-base(|) { * }
  2729. multi sub parse-base(Str:D $str, Int:D $radix) { $str.parse-base($radix) }
  2730. sub parse-names(Str:D $str) { $str.parse-names }
  2731. proto sub substr(|) { * }
  2732. multi sub substr(Str:D \what, Int:D \start) {
  2733. my str $str = nqp::unbox_s(what);
  2734. my int $max = nqp::chars($str);
  2735. my int $from = nqp::unbox_i(start);
  2736. Rakudo::Internals.SUBSTR-START-OOR($from,$max).fail
  2737. if nqp::islt_i($from,0) || nqp::isgt_i($from,$max);
  2738. nqp::p6box_s(nqp::substr($str,$from));
  2739. }
  2740. multi sub substr(Str:D \what, Callable:D \start) {
  2741. my str $str = nqp::unbox_s(what);
  2742. my int $max = nqp::chars($str);
  2743. my int $from = nqp::unbox_i((start)(nqp::p6box_i($max)));
  2744. Rakudo::Internals.SUBSTR-START-OOR($from,$max).fail
  2745. if nqp::islt_i($from,0) || nqp::isgt_i($from,$max);
  2746. nqp::p6box_s(nqp::substr($str,$from));
  2747. }
  2748. multi sub substr(Str:D \what, Int:D \start, Int:D \want) {
  2749. my str $str = nqp::unbox_s(what);
  2750. my int $max = nqp::chars($str);
  2751. my int $from = nqp::unbox_i(start);
  2752. Rakudo::Internals.SUBSTR-START-OOR($from,$max).fail
  2753. if nqp::islt_i($from,0) || nqp::isgt_i($from,$max);
  2754. my int $chars = nqp::unbox_i(want);
  2755. Rakudo::Internals.SUBSTR-CHARS-OOR($chars).fail
  2756. if nqp::islt_i($chars,0);
  2757. nqp::p6box_s(nqp::substr($str,$from,$chars));
  2758. }
  2759. multi sub substr(Str() $what, \start, $want?) {
  2760. # should really be int, but \ then doesn't work for rw access
  2761. my $r := Rakudo::Internals.SUBSTR-SANITY($what, start, $want, my Int $from, my Int $chars);
  2762. nqp::istype($r,Failure)
  2763. ?? $r
  2764. !! nqp::p6box_s(nqp::substr(
  2765. nqp::unbox_s($what),nqp::unbox_i($from),nqp::unbox_i($chars)
  2766. ))
  2767. }
  2768. sub substr-rw(\what, \start, $want?) is rw {
  2769. my $Str := nqp::istype(what,Str) ?? what !! what.Str;
  2770. # should really be int, but \ then doesn't work for rw access
  2771. my $r := Rakudo::Internals.SUBSTR-SANITY($Str, start, $want, my Int $from, my Int $chars);
  2772. nqp::istype($r,Failure)
  2773. ?? $r
  2774. !! Proxy.new(
  2775. FETCH => sub ($) {
  2776. nqp::p6box_s(nqp::substr(
  2777. nqp::unbox_s($Str), nqp::unbox_i($from), nqp::unbox_i($chars)
  2778. ));
  2779. },
  2780. STORE => sub ($, Str() $new) {
  2781. my $str = nqp::unbox_s($Str);
  2782. what = nqp::p6box_s(
  2783. nqp::concat(
  2784. nqp::substr($str,0,nqp::unbox_i($from)),
  2785. nqp::concat(
  2786. nqp::unbox_s($new),
  2787. nqp::substr($str,nqp::unbox_i($from + $chars))
  2788. )
  2789. )
  2790. );
  2791. },
  2792. )
  2793. }
  2794. multi sub infix:<eqv>(Str:D \a, Str:D \b) {
  2795. nqp::p6bool(
  2796. nqp::unless(
  2797. nqp::eqaddr(a,b),
  2798. nqp::eqaddr(a.WHAT,b.WHAT) && nqp::iseq_s(a,b)
  2799. )
  2800. )
  2801. }
  2802. proto sub samemark(|) {*}
  2803. multi sub samemark($s, $pat) { $s.samemark($pat) }