Changeset 7257
- Timestamp:
- 07/31/07 04:59:10 (1 year ago)
- Files:
-
- trunk/activesupport/CHANGELOG (modified) (1 diff)
- trunk/activesupport/lib/active_support/multibyte/handlers/utf8_handler.rb (modified) (1 diff)
- trunk/activesupport/test/multibyte_handler_test.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activesupport/CHANGELOG
r7217 r7257 1 1 *SVN* 2 3 * Add support for []= on ActiveSupport::Multibyte::Chars. Closes #9142. [ewan, manfred] 2 4 3 5 * Added Array#extract_options! to encapsulate the pattern of getting an options hash out of a variable number of parameters #8759 [norbert]. trunk/activesupport/lib/active_support/multibyte/handlers/utf8_handler.rb
r6044 r7257 139 139 bidx = str.index(*args) 140 140 bidx ? (u_unpack(str.slice(0...bidx)).size) : nil 141 end 142 143 # Works just like the indexed replace method on string, except instead of byte offsets you specify 144 # character offsets. 145 # 146 # Example: 147 # 148 # s = "MÌller" 149 # s.chars[2] = "e" # Replace character with offset 2 150 # s 151 # #=> "MÌeler" 152 # 153 # s = "MÌller" 154 # s.chars[1, 2] = "ö" # Replace 2 characters at character offset 1 155 # s 156 # #=> "Möler" 157 def []=(str, *args) 158 replace_by = args.pop 159 # Indexed replace with regular expressions already works 160 return str[*args] = replace_by if args.first.is_a?(Regexp) 161 result = u_unpack(str) 162 if args[0].is_a?(Fixnum) 163 raise IndexError, "index #{args[0]} out of string" if args[0] >= result.length 164 min = args[0] 165 max = args[1].nil? ? min : (min + args[1] - 1) 166 range = Range.new(min, max) 167 replace_by = [replace_by].pack('U') if replace_by.is_a?(Fixnum) 168 elsif args.first.is_a?(Range) 169 raise RangeError, "#{args[0]} out of range" if args[0].min >= result.length 170 range = args[0] 171 else 172 needle = args[0].to_s 173 min = index(str, needle) 174 max = min + length(needle) - 1 175 range = Range.new(min, max) 176 end 177 result[range] = u_unpack(replace_by) 178 str.replace(result.pack('U*')) 141 179 end 142 180 trunk/activesupport/test/multibyte_handler_test.rb
r6021 r7257 200 200 end 201 201 202 def test_indexed_insert 203 s = "Îαλη!" 204 @handler[s, 2] = "a" 205 assert_equal "Îαaη!", s 206 @handler[s, 2] = "ηη" 207 assert_equal "Îαηηη!", s 208 assert_raises(IndexError) { @handler[s, 10] = 'a' } 209 assert_equal "Îαηηη!", s 210 @handler[s, 2] = 32 211 assert_equal "Îα ηη!", s 212 @handler[s, 3, 2] = "λλλ" 213 assert_equal "Îα λλλ!", s 214 @handler[s, 1, 0] = "λ" 215 assert_equal "Îλα λλλ!", s 216 assert_raises(IndexError) { @handler[s, 10, 4] = 'a' } 217 assert_equal "Îλα λλλ!", s 218 @handler[s, 4..6] = "ηη" 219 assert_equal "Îλα ηη!", s 220 assert_raises(RangeError) { @handler[s, 10..12] = 'a' } 221 assert_equal "Îλα ηη!", s 222 @handler[s, /ηη/] = "λλλ" 223 assert_equal "Îλα λλλ!", s 224 assert_raises(IndexError) { @handler[s, /ii/] = 'a' } 225 assert_equal "Îλα λλλ!", s 226 @handler[s, /(λλ)(.)/, 2] = "α" 227 assert_equal "Îλα λλα!", s 228 assert_raises(IndexError) { @handler[s, /()/, 10] = 'a' } 229 assert_equal "Îλα λλα!", s 230 @handler[s, "α"] = "η" 231 assert_equal "Îλη λλα!", s 232 @handler[s, "λλ"] = "ααα" 233 assert_equal "Îλη αααα!", s 234 end 235 202 236 def test_strip 203 237 # A unicode aware version of strip should strip all 26 types of whitespace. This includes the NO BREAK SPACE