5
5
"bytes"
6
6
"errors"
7
7
"io"
8
+ "unicode/utf8"
8
9
9
10
"github.com/xlab/at/pdu"
10
11
)
@@ -119,20 +120,11 @@ func (s *Message) encodeDeliver(buf *bytes.Buffer) (n int, err error) {
119
120
sms .ProtocolIdentifier = 0x00 // Short Message Type 0
120
121
sms .DataCodingScheme = byte (s .Encoding )
121
122
sms .ServiceCentreTimestamp = s .ServiceCenterTime .PDU ()
122
-
123
- var userData []byte
124
- switch s .Encoding {
125
- case Encodings .Gsm7Bit , Encodings .Gsm7Bit_2 :
126
- userData = pdu .Encode7Bit (s .Text )
127
- sms .UserDataLength = byte (len (s .Text ))
128
- case Encodings .UCS2 :
129
- userData = pdu .EncodeUcs2 (s .Text )
130
- sms .UserDataLength = byte (len (userData ))
131
- default :
132
- return 0 , ErrUnknownEncoding
123
+ sms .UserData , sms .UserDataLength , err = s .encodedUserData ()
124
+ if err != nil {
125
+ return 0 , err
133
126
}
134
127
135
- sms .UserData = userData
136
128
return buf .Write (sms .Bytes ())
137
129
}
138
130
@@ -165,19 +157,10 @@ func (s *Message) encodeSubmit(buf *bytes.Buffer) (n int, err error) {
165
157
return 0 , ErrNonRelative
166
158
}
167
159
168
- var userData []byte
169
- switch s .Encoding {
170
- case Encodings .Gsm7Bit , Encodings .Gsm7Bit_2 :
171
- userData = pdu .Encode7Bit (s .Text )
172
- sms .UserDataLength = byte (len (s .Text ))
173
- case Encodings .UCS2 :
174
- userData = pdu .EncodeUcs2 (s .Text )
175
- sms .UserDataLength = byte (len (userData ))
176
- default :
177
- return 0 , ErrUnknownEncoding
160
+ sms .UserData , sms .UserDataLength , err = s .encodedUserData ()
161
+ if err != nil {
162
+ return 0 , err
178
163
}
179
-
180
- sms .UserData = userData
181
164
return buf .Write (sms .Bytes ())
182
165
}
183
166
@@ -202,20 +185,11 @@ func (s *Message) encodeStatusReport(buf *bytes.Buffer) (n int, err error) {
202
185
sms .ServiceCentreTimestamp = s .ServiceCenterTime .PDU ()
203
186
sms .DischargeTimestamp = s .DischargeTime .PDU ()
204
187
sms .Status = byte (s .Status )
205
-
206
- var userData []byte
207
- switch s .Encoding {
208
- case Encodings .Gsm7Bit , Encodings .Gsm7Bit_2 :
209
- userData = pdu .Encode7Bit (s .Text )
210
- sms .UserDataLength = byte (len (s .Text ))
211
- case Encodings .UCS2 :
212
- userData = pdu .EncodeUcs2 (s .Text )
213
- sms .UserDataLength = byte (len (userData ))
214
- default :
215
- return 0 , ErrUnknownEncoding
188
+ sms .UserData , sms .UserDataLength , err = s .encodedUserData ()
189
+ if err != nil {
190
+ return 0 , err
216
191
}
217
192
218
- sms .UserData = userData
219
193
return buf .Write (sms .Bytes ())
220
194
}
221
195
@@ -345,6 +319,21 @@ func (s *Message) decodeStatusReport(data []byte) (n int, err error) {
345
319
return n , err
346
320
}
347
321
322
+ func (s * Message ) encodedUserData () (userData []byte , length byte , err error ) {
323
+ switch s .Encoding {
324
+ case Encodings .Gsm7Bit , Encodings .Gsm7Bit_2 :
325
+ userData = pdu .Encode7Bit (s .Text )
326
+ length = byte (utf8 .RuneCountInString (s .Text ))
327
+ case Encodings .UCS2 :
328
+ userData = pdu .EncodeUcs2 (s .Text )
329
+ length = byte (len (userData ))
330
+ default :
331
+ err = ErrUnknownEncoding
332
+ }
333
+
334
+ return
335
+ }
336
+
348
337
func (s * Message ) decodeUserData (data []byte , dataLen byte ) (err error ) {
349
338
switch s .Encoding {
350
339
case Encodings .Gsm7Bit , Encodings .Gsm7Bit_2 :
0 commit comments