File tree 3 files changed +29
-4
lines changed
3 files changed +29
-4
lines changed Original file line number Diff line number Diff line change @@ -86,15 +86,25 @@ public function setPosition(Point $point): self
86
86
}
87
87
88
88
/**
89
- * Count segments of line
90
- *
89
+ * Count segments (individual words including punctuation marks) of line
90
+ *
91
91
* @return int
92
92
*/
93
93
public function count (): int
94
94
{
95
95
return count ($ this ->segments );
96
96
}
97
97
98
+ /**
99
+ * Count characters of line
100
+ *
101
+ * @return int
102
+ */
103
+ public function length (): int
104
+ {
105
+ return mb_strlen ((string ) $ this );
106
+ }
107
+
98
108
/**
99
109
* Cast line to string
100
110
*
Original file line number Diff line number Diff line change @@ -62,10 +62,10 @@ public function longestLine(): Line
62
62
{
63
63
$ lines = $ this ->lines ();
64
64
usort ($ lines , function (Line $ a , Line $ b ) {
65
- if (mb_strlen (( string ) $ a ) === mb_strlen (( string ) $ b )) {
65
+ if ($ a -> length ( ) === $ b -> length ( )) {
66
66
return 0 ;
67
67
}
68
- return mb_strlen (( string ) $ a ) > mb_strlen (( string ) $ b ) ? -1 : 1 ;
68
+ return $ a -> length () > $ b -> length ( ) ? -1 : 1 ;
69
69
});
70
70
71
71
return $ lines [0 ];
Original file line number Diff line number Diff line change @@ -45,6 +45,21 @@ public function testCount(): void
45
45
$ this ->assertEquals (2 , $ line ->count ());
46
46
}
47
47
48
+ public function testLength (): void
49
+ {
50
+ $ line = new Line ();
51
+ $ this ->assertEquals (0 , $ line ->length ());
52
+
53
+ $ line = new Line ("foo " );
54
+ $ this ->assertEquals (3 , $ line ->length ());
55
+
56
+ $ line = new Line ("foo bar. " );
57
+ $ this ->assertEquals (8 , $ line ->length ());
58
+
59
+ $ line = new Line ("🫷🙂🫸 " );
60
+ $ this ->assertEquals (3 , $ line ->length ());
61
+ }
62
+
48
63
public function testAdd (): void
49
64
{
50
65
$ line = new Line ();
You can’t perform that action at this time.
0 commit comments