That's pushing a lot of data around, and, I expect, causing you to start getting cache misses on the largest size string (although evidently the smaller strings were able to fit inside the cache comfortably). Try using a solution like this one.
def newSplitter(s, n = 16):
for i in xrange(0, len(s), n):
yield l[i: i + n]
Rob Pike 26 September 2013
var buffer[256] byte
var slice[] byte = buffer[100: 150]
func AddOneToEachElement(slice[] byte) {
for i: = range slice {
slice[i]++
}
}
// +build OMIT
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import(
"fmt"
)
var buffer[256] byte
var slice[] byte = buffer[100: 150]
func AddOneToEachElement(slice[] byte) {
for i: = range slice {
slice[i]++
}
}
// +build OMIT
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
)
var buffer [256]byte
var slice []byte = buffer[100:150]
func AddOneToEachElement(slice []byte) {
for i := range slice {
slice[i]++
}
}
func main() {
slice: = buffer[10: 20]
for i: = 0;i < len(slice);i++{
slice[i] = byte(i)
}
fmt.Println("before", slice)
AddOneToEachElement(slice)
fmt.Println("after", slice)
}
// +build OMIT
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import(
"fmt"
)
var buffer[256] byte
var slice[] byte = buffer[100: 150]
// +build OMIT
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
)
var buffer [256]byte
var slice []byte = buffer[100:150]
func SubtractOneFromLength(slice[] byte)[] byte {
slice = slice[0: len(slice) - 1]
return slice
}
func main() {
fmt.Println("Before: len(slice) =", len(slice))
newSlice: = SubtractOneFromLength(slice)
fmt.Println("After: len(slice) =", len(slice))
fmt.Println("After: len(newSlice) =", len(newSlice))
}
// +build OMIT
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import(
"fmt"
)
var buffer[256] byte
var slice[] byte = buffer[100: 150]
// +build OMIT
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
)
var buffer [256]byte
var slice []byte = buffer[100:150]
func PtrSubtractOneFromLength(slicePtr * [] byte) {
slice: = * slicePtr *
slicePtr = slice[0: len(slice) - 1]
}
func main() {
fmt.Println("Before: len(slice) =", len(slice))
PtrSubtractOneFromLength( & slice)
fmt.Println("After: len(slice) =", len(slice))
}
Returns a soundex string from str, or NULL if str is NULL. Two strings that sound almost the same should have identical soundex strings. A standard soundex string is four characters long, but the SOUNDEX() function returns an arbitrarily long string. You can use SUBSTRING() on the result to get a standard soundex string. All nonalphabetic characters in str are ignored. All international alphabetic characters outside the A-Z range are treated as vowels. , For all forms of SUBSTRING(), the position of the first character in the string from which the substring is to be extracted is reckoned as 1. , Returns the string str with all characters changed to uppercase according to the current character set mapping, or NULL if str is NULL. The default character set is utf8mb4. , Returns the string str with all characters changed to lowercase according to the current character set mapping, or NULL if str is NULL. The default character set is utf8mb4.
Returns the numeric value of the leftmost character of the
string str
. Returns
0
if str
is the
empty string. Returns NULL
if
str
is NULL
.
ASCII()
works for 8-bit
characters.
mysql > SELECT ASCII('2'); -
> 50
mysql > SELECT ASCII(2); -
> 50
mysql > SELECT ASCII('dx'); -
> 100
Returns a string representation of the binary value of
N
, where
N
is a longlong
(BIGINT
) number. This is
equivalent to
CONV(
.
Returns N
,10,2)NULL
if
N
is NULL
.
mysql > SELECT BIN(12); -
> '1100'
Returns the length of the string
str
in bits. Returns
NULL
if str
is
NULL
.
mysql > SELECT BIT_LENGTH('text'); -
> 32
CHAR()
interprets each argument
N
as an integer and returns a
string consisting of the characters given by the code values
of those integers. NULL
values are skipped.
mysql > SELECT CHAR(77, 121, 83, 81, '76'); +
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
|
CHAR(77, 121, 83, 81, '76') |
+ -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
|
0x4D7953514C |
+ -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
1 row in set(0.00 sec)
mysql > SELECT CHAR(77, 77.3, '77.3'); +
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
|
CHAR(77, 77.3, '77.3') |
+ -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
|
0x4D4D4D |
+ -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
1 row in set(0.00 sec)
By default, CHAR()
returns a
binary string. To produce a string in a given character set,
use the optional USING
clause:
mysql > SELECT CHAR(77, 121, 83, 81, '76'
USING utf8mb4); +
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - +
|
CHAR(77, 121, 83, 81, '76'
USING utf8mb4) |
+ -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - +
|
MySQL |
+ -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - +
1 row in set(0.00 sec)
mysql > SELECT CHAR(77, 77.3, '77.3'
USING utf8mb4); +
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
|
CHAR(77, 77.3, '77.3'
USING utf8mb4) |
+ -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
|
MMM |
+ -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
1 row in set, 1 warning(0.00 sec)
mysql > SHOW WARNINGS; +
-- -- -- -- - + -- -- -- + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - +
|
Level | Code | Message |
+ -- -- -- -- - + -- -- -- + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - +
|
Warning | 1292 | Truncated incorrect INTEGER value: '77.3' |
+ -- -- -- -- - + -- -- -- + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - +
1 row in set(0.00 sec)
CHAR()
arguments larger than
255 are converted into multiple result bytes. For example,
CHAR(256)
is equivalent to
CHAR(1,0)
, and
CHAR(256*256)
is equivalent to
CHAR(1,0,0)
:
mysql > SELECT HEX(CHAR(1, 0)), HEX(CHAR(256)); + -- -- -- -- -- -- -- -- + -- -- -- -- -- -- -- -- + | HEX(CHAR(1, 0)) | HEX(CHAR(256)) | + -- -- -- -- -- -- -- -- + -- -- -- -- -- -- -- -- + | 0100 | 0100 | + -- -- -- -- -- -- -- -- + -- -- -- -- -- -- -- -- + 1 row in set(0.00 sec) mysql > SELECT HEX(CHAR(1, 0, 0)), HEX(CHAR(256 * 256)); + -- -- -- -- -- -- -- -- -- + -- -- -- -- -- -- -- -- -- -- + | HEX(CHAR(1, 0, 0)) | HEX(CHAR(256 * 256)) | + -- -- -- -- -- -- -- -- -- + -- -- -- -- -- -- -- -- -- -- + | 010000 | 010000 | + -- -- -- -- -- -- -- -- -- + -- -- -- -- -- -- -- -- -- -- + 1 row in set(0.00 sec)
Returns the length of the string
str
, measured in code points. A
multibyte character counts as a single code point. This means
that, for a string containing two 3-byte characters,
LENGTH()
returns
6
, whereas
CHAR_LENGTH()
returns
2
, as shown here:
mysql > SET @dolphin: = '海豚';
Query OK, 0 rows affected(0.01 sec)
mysql > SELECT LENGTH(@dolphin), CHAR_LENGTH(@dolphin); +
-- -- -- -- -- -- -- -- -- + -- -- -- -- -- -- -- -- -- -- -- - +
|
LENGTH(@dolphin) | CHAR_LENGTH(@dolphin) |
+ -- -- -- -- -- -- -- -- -- + -- -- -- -- -- -- -- -- -- -- -- - +
|
6 | 2 |
+ -- -- -- -- -- -- -- -- -- + -- -- -- -- -- -- -- -- -- -- -- - +
1 row in set(0.00 sec)
CONCAT()
returns
NULL
if any argument is
NULL
.
mysql > SELECT CONCAT('My', 'S', 'QL'); -
> 'MySQL'
mysql > SELECT CONCAT('My', NULL, 'QL'); -
> NULL
mysql > SELECT CONCAT(14.3); -
> '14.3'
For quoted strings, concatenation can be performed by placing the strings next to each other:
mysql > SELECT 'My'
'S'
'QL'; -
> 'MySQL'
All behavior is based on the expected behavior of the JavaScript API Array.prototype.slice() and String.prototype.slice().,When operating on an Array, the returned Array is always a copy even when all the elements are being returned.,Creates a new Array or String containing a subset (slice) of the elements.,When operating on a blank value, the pipe returns the blank value.
{ { value_expression | slice: start[: end] } }
CommonModule
content_copy
@Component({
selector: 'slice-list-pipe',
template: `<ul>
<li *ngFor="let i of collection | slice:1:3">{{i}}</li>
</ul>`
})
export class SlicePipeListComponent {
collection: string[] = ['a', 'b', 'c', 'd'];
}
content_copy
<li>b</li>
<li>c</li>
content_copy
@Component({
selector: 'slice-string-pipe',
template: `<div>
<p>{{str}}[0:4]: '{{str | slice:0:4}}' - output is expected to be 'abcd'</p>
<p>{{str}}[4:0]: '{{str | slice:4:0}}' - output is expected to be ''</p>
<p>{{str}}[-4]: '{{str | slice:-4}}' - output is expected to be 'ghij'</p>
<p>{{str}}[-4:-2]: '{{str | slice:-4:-2}}' - output is expected to be 'gh'</p>
<p>{{str}}[-100]: '{{str | slice:-100}}' - output is expected to be 'abcdefghij'</p>
<p>{{str}}[100]: '{{str | slice:100}}' - output is expected to be ''</p>
</div>`
})
export class SlicePipeStringComponent {
str: string = 'abcdefghij';
}
A second variant of Span<T>, called System.ReadOnlySpan<T>, enables read-only access. This type is just like Span<T>, except its indexer takes advantage of a new C# 7.2 feature to return a “ref readonly T” instead of a “ref T,” enabling it to work with immutable data types like System.String. ReadOnlySpan<T> makes it very efficient to slice strings without allocating or copying, as shown here:,From there, you can easily and efficiently create a span to represent/point to just a subset of this array, utilizing an overload of the span’s Slice method. From there you can index into the resulting span to write and read data in the relevant portion of the original array:,The runtime applies similar optimizations to span (both Span<T> and ReadOnlySpan<T>). Compare the previous example to the following code, where the only change is on the parameter type:,You could instead use stack-allocation, and even take advantage of Span<char>, to avoid needing to use unsafe code. This approach also takes advantage of the new string constructor that accepts a ReadOnlySpan<char>, like so:
For example, you can create a Span<T> from an array:
var arr = new byte[10];
Span<byte> bytes = arr; // Implicit cast from T[] to Span<T>
From there, you can easily and efficiently create a span to represent/point to just a subset of this array, utilizing an overload of the span’s Slice method. From there you can index into the resulting span to write and read data in the relevant portion of the original array:
Span<byte> slicedBytes = bytes.Slice(start: 5, length: 2);
slicedBytes[0] = 42;
slicedBytes[1] = 43;
Assert.Equal(42, slicedBytes[0]);
Assert.Equal(43, slicedBytes[1]);
Assert.Equal(arr[5], slicedBytes[0]);
Assert.Equal(arr[6], slicedBytes[1]);
slicedBytes[2] = 44; // Throws IndexOutOfRangeException
bytes[2] = 45; // OK
Assert.Equal(arr[2], bytes[2]);
Assert.Equal(45, arr[2]);
As mentioned, spans are more than just a way to access and subset arrays. They can also be used to refer to data on the stack. For example:
Span<byte> bytes = stackalloc byte[2]; // Using C# 7.2 stackalloc support for spans
bytes[0] = 42;
bytes[1] = 43;
Assert.Equal(42, bytes[0]);
Assert.Equal(43, bytes[1]);
bytes[2] = 44; // throws IndexOutOfRangeException
The Span<T> indexer takes advantage of a C# language feature introduced in C# 7.0 called ref returns. The indexer is declared with a “ref T” return type, which provides semantics like that of indexing into arrays, returning a reference to the actual storage location rather than returning a copy of what lives at that location:
public ref T this[int index] {
get {
...
}
}
The impact of this ref-returning indexer is most obvious via example, such as by comparing it with the List<T> indexer, which is not ref returning. Here’s an example:
struct MutableStruct { public int Value; }
...
Span<MutableStruct> spanOfStructs = new MutableStruct[1];
spanOfStructs[0].Value = 42;
Assert.Equal(42, spanOfStructs[0].Value);
var listOfStructs = new List<MutableStruct> { new MutableStruct() };
listOfStructs[0].Value = 42; // Error CS1612: the return value is not a variable
Argument lists can also be used to take arbitrary keyword arguments. The meta.keywords() function takes an argument list and returns any extra keywords that were passed to the function as a map from argument names (not including $) to those arguments’ values.,Because an argument list keeps track of both positional and keyword arguments, you use it to pass both at once to another function. That makes it super easy to define an alias for a function!,Just like argument lists allow functions to take arbitrary positional or keyword arguments, the same syntax can be used to pass positional and keyword arguments to a function. If you pass a list followed by ... as the last argument of a function call, its elements will be treated as additional positional arguments. Similarly, a map followed by ... will be treated as additional keyword arguments. You can even pass both at once!,If you don’t ever pass an argument list to the meta.keywords() function, that argument list won’t allow extra keyword arguments. This helps callers of your function make sure they haven’t accidentally misspelled any argument names.
SCSS Syntax
@function pow($base, $exponent) {
$result: 1;
@for $_ from 1 through $exponent {
$result: $result * $base;
}
@return $result;
}
.sidebar {
float: left;
margin - left: pow(4, 3) * 1 px;
}
Sass Syntax
@function pow($base, $exponent)
$result: 1
@for $_ from 1 through $exponent
$result: $result * $base
@return $result
.sidebar
float: left
margin - left: pow(4, 3) * 1 px
CSS Output
.sidebar {
float: left;
margin - left: 64 px;
}
SCSS Syntax
@function invert($color, $amount: 100 % ) { $inverse: change - color($color, $hue: hue($color) + 180); @return mix($inverse, $color, $amount); } $primary - color: #036; .header { background-color: invert($primary-color, 80%); }
Sass Syntax
@function invert($color, $amount: 100 % )
$inverse: change - color($color, $hue: hue($color) + 180)
@return mix($inverse, $color, $amount)
$primary - color: #036
.header
background-color: invert($primary-color, 80%)
CSS Output
.header {
background - color: #523314;
}