github.com/wangyougui/gf/v2@v2.6.5/text/gstr/gstr_z_unit_pos_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/wangyougui/gf.
     6  
     7  // go test *.go -bench=".*"
     8  
     9  package gstr_test
    10  
    11  import (
    12  	"testing"
    13  
    14  	"github.com/wangyougui/gf/v2/test/gtest"
    15  	"github.com/wangyougui/gf/v2/text/gstr"
    16  )
    17  
    18  func Test_Pos(t *testing.T) {
    19  	gtest.C(t, func(t *gtest.T) {
    20  		s1 := "abcdEFGabcdefg"
    21  		t.Assert(gstr.Pos(s1, "ab"), 0)
    22  		t.Assert(gstr.Pos(s1, "ab", 2), 7)
    23  		t.Assert(gstr.Pos(s1, "abd", 0), -1)
    24  		t.Assert(gstr.Pos(s1, "e", -4), 11)
    25  	})
    26  	gtest.C(t, func(t *gtest.T) {
    27  		s1 := "我爱China very much"
    28  		t.Assert(gstr.Pos(s1, "爱"), 3)
    29  		t.Assert(gstr.Pos(s1, "C"), 6)
    30  		t.Assert(gstr.Pos(s1, "China"), 6)
    31  	})
    32  }
    33  
    34  func Test_PosRune(t *testing.T) {
    35  	gtest.C(t, func(t *gtest.T) {
    36  		s1 := "abcdEFGabcdefg"
    37  		t.Assert(gstr.PosRune(s1, "ab"), 0)
    38  		t.Assert(gstr.PosRune(s1, "ab", 2), 7)
    39  		t.Assert(gstr.PosRune(s1, "abd", 0), -1)
    40  		t.Assert(gstr.PosRune(s1, "e", -4), 11)
    41  	})
    42  	gtest.C(t, func(t *gtest.T) {
    43  		s1 := "我爱China very much"
    44  		t.Assert(gstr.PosRune(s1, "爱"), 1)
    45  		t.Assert(gstr.PosRune(s1, "C"), 2)
    46  		t.Assert(gstr.PosRune(s1, "China"), 2)
    47  	})
    48  }
    49  
    50  func Test_PosI(t *testing.T) {
    51  	gtest.C(t, func(t *gtest.T) {
    52  		s1 := "abcdEFGabcdefg"
    53  		t.Assert(gstr.PosI(s1, "zz"), -1)
    54  		t.Assert(gstr.PosI(s1, "ab"), 0)
    55  		t.Assert(gstr.PosI(s1, "ef", 2), 4)
    56  		t.Assert(gstr.PosI(s1, "abd", 0), -1)
    57  		t.Assert(gstr.PosI(s1, "E", -4), 11)
    58  	})
    59  	gtest.C(t, func(t *gtest.T) {
    60  		s1 := "我爱China very much"
    61  		t.Assert(gstr.PosI(s1, "爱"), 3)
    62  		t.Assert(gstr.PosI(s1, "c"), 6)
    63  		t.Assert(gstr.PosI(s1, "china"), 6)
    64  	})
    65  }
    66  
    67  func Test_PosIRune(t *testing.T) {
    68  	gtest.C(t, func(t *gtest.T) {
    69  		s1 := "abcdEFGabcdefg"
    70  		t.Assert(gstr.PosIRune(s1, "zz"), -1)
    71  		t.Assert(gstr.PosIRune(s1, "ab"), 0)
    72  		t.Assert(gstr.PosIRune(s1, "ef", 2), 4)
    73  		t.Assert(gstr.PosIRune(s1, "abd", 0), -1)
    74  		t.Assert(gstr.PosIRune(s1, "E", -4), 11)
    75  	})
    76  	gtest.C(t, func(t *gtest.T) {
    77  		s1 := "我爱China very much"
    78  		t.Assert(gstr.PosIRune(s1, "爱"), 1)
    79  		t.Assert(gstr.PosIRune(s1, "c"), 2)
    80  		t.Assert(gstr.PosIRune(s1, "china"), 2)
    81  	})
    82  }
    83  
    84  func Test_PosR(t *testing.T) {
    85  	gtest.C(t, func(t *gtest.T) {
    86  		s1 := "abcdEFGabcdefg"
    87  		s2 := "abcdEFGz1cdeab"
    88  		t.Assert(gstr.PosR(s1, "zz"), -1)
    89  		t.Assert(gstr.PosR(s1, "ab"), 7)
    90  		t.Assert(gstr.PosR(s2, "ab", -2), 0)
    91  		t.Assert(gstr.PosR(s1, "ef"), 11)
    92  		t.Assert(gstr.PosR(s1, "abd", 0), -1)
    93  		t.Assert(gstr.PosR(s1, "e", -4), -1)
    94  	})
    95  	gtest.C(t, func(t *gtest.T) {
    96  		s1 := "我爱China very much"
    97  		t.Assert(gstr.PosR(s1, "爱"), 3)
    98  		t.Assert(gstr.PosR(s1, "C"), 6)
    99  		t.Assert(gstr.PosR(s1, "China"), 6)
   100  	})
   101  }
   102  
   103  func Test_PosRRune(t *testing.T) {
   104  	gtest.C(t, func(t *gtest.T) {
   105  		s1 := "abcdEFGabcdefg"
   106  		s2 := "abcdEFGz1cdeab"
   107  		t.Assert(gstr.PosRRune(s1, "zz"), -1)
   108  		t.Assert(gstr.PosRRune(s1, "ab"), 7)
   109  		t.Assert(gstr.PosRRune(s2, "ab", -2), 0)
   110  		t.Assert(gstr.PosRRune(s1, "ef"), 11)
   111  		t.Assert(gstr.PosRRune(s1, "abd", 0), -1)
   112  		t.Assert(gstr.PosRRune(s1, "e", -4), -1)
   113  	})
   114  	gtest.C(t, func(t *gtest.T) {
   115  		s1 := "我爱China very much"
   116  		t.Assert(gstr.PosRRune(s1, "爱"), 1)
   117  		t.Assert(gstr.PosRRune(s1, "C"), 2)
   118  		t.Assert(gstr.PosRRune(s1, "China"), 2)
   119  	})
   120  }
   121  
   122  func Test_PosRI(t *testing.T) {
   123  	gtest.C(t, func(t *gtest.T) {
   124  		s1 := "abcdEFGabcdefg"
   125  		s2 := "abcdEFGz1cdeab"
   126  		t.Assert(gstr.PosRI(s1, "zz"), -1)
   127  		t.Assert(gstr.PosRI(s1, "AB"), 7)
   128  		t.Assert(gstr.PosRI(s2, "AB", -2), 0)
   129  		t.Assert(gstr.PosRI(s1, "EF"), 11)
   130  		t.Assert(gstr.PosRI(s1, "abd", 0), -1)
   131  		t.Assert(gstr.PosRI(s1, "e", -5), 4)
   132  	})
   133  	gtest.C(t, func(t *gtest.T) {
   134  		s1 := "我爱China very much"
   135  		t.Assert(gstr.PosRI(s1, "爱"), 3)
   136  		t.Assert(gstr.PosRI(s1, "C"), 19)
   137  		t.Assert(gstr.PosRI(s1, "China"), 6)
   138  	})
   139  }
   140  
   141  func Test_PosRIRune(t *testing.T) {
   142  	gtest.C(t, func(t *gtest.T) {
   143  		s1 := "abcdEFGabcdefg"
   144  		s2 := "abcdEFGz1cdeab"
   145  		t.Assert(gstr.PosRIRune(s1, "zz"), -1)
   146  		t.Assert(gstr.PosRIRune(s1, "AB"), 7)
   147  		t.Assert(gstr.PosRIRune(s2, "AB", -2), 0)
   148  		t.Assert(gstr.PosRIRune(s1, "EF"), 11)
   149  		t.Assert(gstr.PosRIRune(s1, "abd", 0), -1)
   150  		t.Assert(gstr.PosRIRune(s1, "e", -5), 4)
   151  	})
   152  	gtest.C(t, func(t *gtest.T) {
   153  		s1 := "我爱China very much"
   154  		t.Assert(gstr.PosRIRune(s1, "爱"), 1)
   155  		t.Assert(gstr.PosRIRune(s1, "C"), 15)
   156  		t.Assert(gstr.PosRIRune(s1, "China"), 2)
   157  	})
   158  }