From 864188e24c5d7d0add1cbf7b6ec2659a5244a3a2 Mon Sep 17 00:00:00 2001 From: Thibaut Date: Fri, 13 Dec 2013 11:21:38 +0000 Subject: [PATCH] Use String#sub instead of String#gsub when possible --- lib/docs/core/filter.rb | 2 +- lib/docs/core/subscriber.rb | 4 ++-- lib/docs/filters/backbone/entries.rb | 2 +- lib/docs/filters/core/clean_html.rb | 2 -- lib/docs/filters/http/entries.rb | 2 +- lib/docs/filters/sass/clean_html.rb | 2 +- lib/docs/subscribers/filter_subscriber.rb | 2 +- lib/tasks/docs.thor | 2 +- test/lib/docs/storage/file_store_test.rb | 2 +- 9 files changed, 9 insertions(+), 11 deletions(-) diff --git a/lib/docs/core/filter.rb b/lib/docs/core/filter.rb index 0209d7b2..49410caf 100644 --- a/lib/docs/core/filter.rb +++ b/lib/docs/core/filter.rb @@ -43,7 +43,7 @@ module Docs end def slug - @slug ||= subpath.gsub(/\A\//, '').gsub('.html', '') + @slug ||= subpath.sub(/\A\//, '').sub(/\.html\z/, '') end def root_page? diff --git a/lib/docs/core/subscriber.rb b/lib/docs/core/subscriber.rb index b1c7a2e2..5c21acb5 100644 --- a/lib/docs/core/subscriber.rb +++ b/lib/docs/core/subscriber.rb @@ -17,11 +17,11 @@ module Docs end def format_url(url) - url.to_s.gsub %r{https?://}, '' + url.to_s.sub %r{\Ahttps?://}, '' end def format_path(path) - path.to_s.gsub File.join(File.expand_path('.'), ''), '' + path.to_s.sub File.join(File.expand_path('.'), ''), '' end def justify(str) diff --git a/lib/docs/filters/backbone/entries.rb b/lib/docs/filters/backbone/entries.rb index aca15d26..694f4c67 100644 --- a/lib/docs/filters/backbone/entries.rb +++ b/lib/docs/filters/backbone/entries.rb @@ -8,7 +8,7 @@ module Docs css('[id]').each do |node| # Module if node.name == 'h2' - type = node.content.gsub 'Backbone.', '' + type = node.content.sub 'Backbone.', '' if type.capitalize! # sync, history entries << [node.content, node['id'], type] end diff --git a/lib/docs/filters/core/clean_html.rb b/lib/docs/filters/core/clean_html.rb index 3c575d17..008408a5 100644 --- a/lib/docs/filters/core/clean_html.rb +++ b/lib/docs/filters/core/clean_html.rb @@ -6,8 +6,6 @@ module Docs xpath('./text()', './/text()[not(ancestor::pre) and not(ancestor::code)]').each do |node| content = node.content next unless content.valid_encoding? - content.gsub! %r{\A[[:space:]]+}, ' ' - content.gsub! %r{[[:space:]]+\z}, ' ' content.gsub! %r{[[:space:]]+}, ' ' node.content = content end diff --git a/lib/docs/filters/http/entries.rb b/lib/docs/filters/http/entries.rb index 6bb7a606..3d65a606 100644 --- a/lib/docs/filters/http/entries.rb +++ b/lib/docs/filters/http/entries.rb @@ -2,7 +2,7 @@ module Docs class Http class EntriesFilter < Docs::EntriesFilter def get_type - at_css('h1').content.gsub(/\A\s*HTTP\s+(.+)\s+Definitions\s*\z/, '\1').pluralize + at_css('h1').content.sub(/\A\s*HTTP\s+(.+)\s+Definitions\s*\z/, '\1').pluralize end def include_default_entry? diff --git a/lib/docs/filters/sass/clean_html.rb b/lib/docs/filters/sass/clean_html.rb index f183efe0..b3c48d07 100644 --- a/lib/docs/filters/sass/clean_html.rb +++ b/lib/docs/filters/sass/clean_html.rb @@ -33,7 +33,7 @@ module Docs # Remove "- " before method names css('.signature', 'span.overload').each do |node| - node.child.content = node.child.content.gsub(/\A\s*-\s*/, '') + node.child.content = node.child.content.sub(/\A\s*-\s*/, '') end # Remove links to type classes (e.g. Number) diff --git a/lib/docs/subscribers/filter_subscriber.rb b/lib/docs/subscribers/filter_subscriber.rb index c9a7db8e..b6bb036b 100644 --- a/lib/docs/subscribers/filter_subscriber.rb +++ b/lib/docs/subscribers/filter_subscriber.rb @@ -3,7 +3,7 @@ module Docs self.namespace = 'html_pipeline' def call_filter(event) - log "Filter: #{event.payload[:filter].gsub('Docs::', '').gsub('Filter', '')} [#{event.duration.round}ms]" + log "Filter: #{event.payload[:filter].sub('Docs::', '').sub('Filter', '')} [#{event.duration.round}ms]" end end end diff --git a/lib/tasks/docs.thor b/lib/tasks/docs.thor index 56200c34..64731909 100644 --- a/lib/tasks/docs.thor +++ b/lib/tasks/docs.thor @@ -17,7 +17,7 @@ class DocsCLI < Thor Docs.all. map { |doc| [doc.to_s.demodulize.underscore, doc] }. each { |pair| max_length = pair.first.length if pair.first.length > max_length }. - each { |pair| puts "#{pair.first.rjust max_length + 1}: #{pair.second.base_url.gsub %r{https?://}, ''}" } + each { |pair| puts "#{pair.first.rjust max_length + 1}: #{pair.second.base_url.sub %r{\Ahttps?://}, ''}" } end desc 'page [path] [--verbose] [--debug]', 'Generate a page (no indexing)' diff --git a/test/lib/docs/storage/file_store_test.rb b/test/lib/docs/storage/file_store_test.rb index f1fc9de6..e638a522 100644 --- a/test/lib/docs/storage/file_store_test.rb +++ b/test/lib/docs/storage/file_store_test.rb @@ -124,7 +124,7 @@ class DocsFileStoreTest < MiniTest::Spec describe "#each" do let :paths do paths = [] - store.each { |path| paths << path.gsub(tmp_path, '') } + store.each { |path| paths << path.sub(tmp_path, '') } paths end