Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Str.capitalize. #348

Closed
p6rt opened this issue Sep 30, 2008 · 5 comments
Closed

Refactor Str.capitalize. #348

p6rt opened this issue Sep 30, 2008 · 5 comments
Labels

Comments

@p6rt
Copy link

p6rt commented Sep 30, 2008

Migrated from rt.perl.org#59476 (status was 'resolved')

Searchable as RT59476$

@p6rt
Copy link
Author

p6rt commented Sep 30, 2008

From @bacek

Hello.

There is suggestion from Pm for Str.capitalize

+

+=item capitalize
+ [...]
+=cut
+
+ .local int pos, is_ws, is_lc
+ pos = 0
+ goto first_char
+ next_grapheme​:
+ if pos == len goto done
+ is_ws = is_cclass .CCLASS_WHITESPACE, tmps, pos
+ if is_ws goto ws
+ advance​:
+ pos += 1
+ goto next_grapheme
+ ws​:
+ pos += 1
+ first_char​:
+ is_lc = is_cclass .CCLASS_LOWERCASE, tmps, pos
+ unless is_lc goto advance
...

This section of code would be much simpler (and more efficient)
by using the C<find_cclass> and C<find_not_cclass opcodes> instead of
individually examining each character one-at-a-time.

--
Bacek

@p6rt
Copy link
Author

p6rt commented Oct 1, 2008

From @azawawi

Hi,

Here is a re-factored version that passes t/spec tests.
To test, please use​:
./perl6 t/spec/S29-str/capitalize.rakudo

./ahmadz &

Index​: src/builtins/any-str.pir

--- src/builtins/any-str.pir (revision 31549)
+++ src/builtins/any-str.pir (working copy)
@​@​ -42,7 +42,6 @​@​

.sub 'capitalize' :method :multi(_)
  .local string tmps
- .local string fchr
  .local pmc retv
  .local int len

@​@​ -54,27 +53,21 @​@​

  downcase tmps

- .local int pos, is_ws, is_lc
+ .local int pos
+ .local string s1
  pos = 0
- goto first_char
- next_grapheme​:
- if pos == len goto done
- is_ws = is_cclass .CCLASS_WHITESPACE, tmps, pos
- if is_ws goto ws
- advance​:
- pos += 1
- goto next_grapheme
- ws​:
- pos += 1
- first_char​:
- is_lc = is_cclass .CCLASS_LOWERCASE, tmps, pos
- unless is_lc goto advance
- $S1 = substr tmps, pos, 1
- upcase $S1
- substr tmps, pos, 1, $S1
- ## the length may have changed after replacement, so measure it
again
+ next_word​:
+ pos = find_cclass .CCLASS_LOWERCASE, tmps, pos, len
+ s1 = substr tmps, pos, 1
+ upcase s1
+ substr tmps, pos, 1, s1
  len = length tmps
- goto advance
+ pos+=1
+ if pos == len goto done
+ pos = find_not_cclass .CCLASS_LOWERCASE, tmps, pos, len
+ if pos == len goto done
+ goto next_word
+
  done​:
  retv = tmps
  .return (retv)

@p6rt
Copy link
Author

p6rt commented Oct 1, 2008

The RT System itself - Status changed from 'new' to 'open'

@p6rt
Copy link
Author

p6rt commented Oct 1, 2008

From @moritz

On Wed Oct 01 11​:25​:25 2008, ahmadz wrote​:

Hi,

Here is a re-factored version that passes t/spec tests.

Thanks, applied as r22479 (and closing ticket now).

@p6rt
Copy link
Author

p6rt commented Oct 1, 2008

@moritz - Status changed from 'open' to 'resolved'

@p6rt p6rt closed this as completed Oct 1, 2008
@p6rt p6rt added the Todo label Jan 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant