-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Find dollar sign in a text string for adding emoji (#458)
* add FindDollarSign function to find emoji index in text string * add FindDollarSign function to find emoji index in text string * move function to util folder/package and make dollar sign more readable * rename function
- Loading branch information
1 parent
d576495
commit 955fcb7
Showing
3 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package util | ||
|
||
import "unicode/utf16" | ||
|
||
/* | ||
When you want to send a text message with emoji, you need to add | ||
$ in the text, and identify the index of the $ in the text. | ||
FindDollarSignIndexInUTF16Text helps you to find the index of the $ in the text. | ||
*/ | ||
|
||
type CharInUTF16 uint16 | ||
|
||
const dollarSign CharInUTF16 = 36 | ||
|
||
func FindDollarSignIndexInUTF16Text(text string) (indexes []int32) { | ||
encoded := utf16.Encode([]rune(text)) | ||
for i, unit := range encoded { | ||
|
||
if unit == uint16(dollarSign) { | ||
indexes = append(indexes, int32(i)) | ||
} | ||
} | ||
return indexes | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package util | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestFindDollarSignIndexInUni16Text(t *testing.T) { | ||
text := "Hello, $ hello こんにちは $, สวัสดีครับ $" | ||
indexes := FindDollarSignIndexInUTF16Text(text) | ||
if len(indexes) != 3 { | ||
t.Errorf("Expected 3, but got %d", len(indexes)) | ||
} | ||
} |