smstools 0.2.1 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7df349a1fd0872910cc9f6d2c23992d33807419968cbc9d68547549ef16a18bd
4
- data.tar.gz: a30dd9cbb8550e475a322317b0cecdcb67c13a09f95449bf392c67d8200eb11c
3
+ metadata.gz: f2cecee4608c47f5abf1cf0a980b3a3a646e358d50a72e6b0f1931f554f86c5f
4
+ data.tar.gz: 46eae0938780419f4672581f4b1105a8d51cc4fe7f6150b2e44fdc3c00f16c6e
5
5
  SHA512:
6
- metadata.gz: edd17e186943ec7b89941e2eaba41279fadc39a27ee987cdb0efc3d4f8f4cbac85bb31a50afbe6d9df94fe7fd9f4498fe561861d9f60b97dd0bd39c49ed19ec8
7
- data.tar.gz: b197d9070437eef8bf732134be1a9b3fcedcf71f71f95e0057d1ae214bde4c1b8d2f00769e58104df02acf776df71cf123bb34ead1d4a828c867a2ff1aae639c
6
+ metadata.gz: 6f40d959431dc1185a989b179c91858363978b5200bba7504e499b214ba8b1493c859eebb3308b343ac8000ec747db89ccfbc664692721315bb68c41f96000a0
7
+ data.tar.gz: 7670ac023de1612cd5e4573ad4879526e5f45e38c871416cef3466e4bbee6d1740d0b2ae760b907d57811f913670862e2907b3ed3c8f268b876085195bed2a61
@@ -1,3 +1,7 @@
1
+ ## 0.2.2 (20 Jan 2021)
2
+
3
+ * #9 Fix the way some complex Unicode characters (like composite emojis) are counted. Thanks to @bryanrite for the neat implementation. Note the fix could be **potentially backwards-incompatible** if you were relying on the incorrect behaviour previously. Technically it's still a bug fix.
4
+
1
5
  ## 0.2.1 (18 Aug 2020)
2
6
 
3
7
  * #7 Introduce `SmsTools.use_ascii_encoding` option (defaults to `true` for backwards-compatibility) that allows disabling the `:ascii` workaround encoding. See #6 and #7 for details. Thanks @kingsley-wang.
data/README.md CHANGED
@@ -159,7 +159,7 @@ Check out the source code of the class to find out more.
159
159
  ### Client-side code
160
160
 
161
161
  If you're using the gem in Rails 3.1 or newer, you can gain access to the `SmsTools.Message` class.
162
- It's interface is similar to the one of `SmsTools::EncodingDetection`. Here is an example in
162
+ Its interface is similar to the one of `SmsTools::EncodingDetection`. Here is an example in
163
163
  CoffeeScript:
164
164
 
165
165
  ```coffeescript
@@ -194,3 +194,9 @@ assumed. It might be possible to use it in other setups as well, but you're on y
194
194
  6. Push to the branch (`git push origin my-new-feature`)
195
195
  7. Send a pull request.
196
196
 
197
+ ## Publishing a new version
198
+
199
+ 1. Pick a version number according to Semantic Versioning.
200
+ 2. Update `CHANGELOG.md`, `version.rb` and potentially this readme.
201
+ 3. Commit the changes, tag them with `vX.Y.Z` (e.g. `v0.2.1`) and push all with `git push --tags`.
202
+ 4. Build and publish the new version of the gem with `gem build smstools.gemspec && gem push *.gem`.
@@ -1,6 +1,7 @@
1
1
  require 'sms_tools/version'
2
2
  require 'sms_tools/encoding_detection'
3
3
  require 'sms_tools/gsm_encoding'
4
+ require 'sms_tools/unicode_encoding'
4
5
 
5
6
  if defined?(::Rails) and ::Rails.version >= '3.1'
6
7
  require 'sms_tools/rails/engine'
@@ -68,8 +68,12 @@ module SmsTools
68
68
  # message, taking into account any double-space symbols in the GSM 03.38
69
69
  # encoding.
70
70
  def length
71
- length = text.length
72
- length += text.chars.count { |char| GsmEncoding.double_byte?(char) } if gsm?
71
+ if unicode?
72
+ length = text.chars.sum { |char| UnicodeEncoding.character_count(char) }
73
+ else
74
+ length = text.length
75
+ length += text.chars.count { |char| GsmEncoding.double_byte?(char) } if gsm?
76
+ end
73
77
 
74
78
  length
75
79
  end
@@ -0,0 +1,15 @@
1
+ module SmsTools
2
+ module UnicodeEncoding
3
+ extend self
4
+
5
+ BASIC_PLANE = 0x0000..0xFFFF
6
+
7
+ # UCS-2/UTF-16 is used for unicode text messaging. UCS-2/UTF-16 represents characters in minimum
8
+ # 2-bytes, any characters in the basic plane are represented with 2-bytes, so each codepoint
9
+ # within the Basic Plane counts as a single character. Any codepoint outside the Basic Plane is
10
+ # encoded using 4-bytes and therefore counts as 2 characters in a text message.
11
+ def character_count(char)
12
+ char.each_codepoint.sum { |codepoint| BASIC_PLANE.include?(codepoint) ? 1 : 2 }
13
+ end
14
+ end
15
+ end
@@ -1,3 +1,3 @@
1
1
  module SmsTools
2
- VERSION = '0.2.1'
2
+ VERSION = '0.2.2'
3
3
  end
@@ -118,6 +118,14 @@ describe SmsTools::EncodingDetection do
118
118
  detection_for('Уникод: Σ: €').length.must_equal 12
119
119
  end
120
120
 
121
+ it "counts ZWJ unicode characters correctly" do
122
+ detection_for('😴').length.must_equal 2
123
+ detection_for('🛌🏽').length.must_equal 4
124
+ detection_for('🤾🏽‍♀️').length.must_equal 7
125
+ detection_for('🇵🇵').length.must_equal 4
126
+ detection_for('👩‍❤️‍👩').length.must_equal 8
127
+ end
128
+
121
129
  describe 'with SmsTools.use_gsm_encoding = false' do
122
130
  before do
123
131
  SmsTools.use_gsm_encoding = false
@@ -166,11 +174,16 @@ describe SmsTools::EncodingDetection do
166
174
  concatenated_parts_for length: 135, encoding: :unicode, must_be: 3
167
175
  end
168
176
 
169
- it "counts parts for actual GSM-encoded and Unicode messages" do
177
+ it "counts parts for actual GSM-encoded messages" do
170
178
  detection_for('').concatenated_parts.must_equal 1
171
- detection_for('Я').concatenated_parts.must_equal 1
172
179
  detection_for('Σ' * 160).concatenated_parts.must_equal 1
173
180
  detection_for('Σ' * 159 + '~').concatenated_parts.must_equal 2
181
+ end
182
+
183
+ it "counts parts for actual Unicode-encoded messages" do
184
+ detection_for('Я').concatenated_parts.must_equal 1
185
+ detection_for('Я' * 70).concatenated_parts.must_equal 1
186
+ detection_for('Я' * 71).concatenated_parts.must_equal 2
174
187
  detection_for('Я' * 133 + '~').concatenated_parts.must_equal 2
175
188
  end
176
189
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smstools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dimitar Dimitrov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-08-18 00:00:00.000000000 Z
11
+ date: 2021-01-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -86,6 +86,7 @@ files:
86
86
  - lib/sms_tools/encoding_detection.rb
87
87
  - lib/sms_tools/gsm_encoding.rb
88
88
  - lib/sms_tools/rails/engine.rb
89
+ - lib/sms_tools/unicode_encoding.rb
89
90
  - lib/sms_tools/version.rb
90
91
  - lib/smstools.rb
91
92
  - smstools.gemspec