//...and its sub method required for passing array slices around private int SubChop2(int element, ArraySlice <int> array) { //Empty collection -> not found if (array.Count == 0) { return(-1); } var index = array.Count / 2; var candidate = array[index]; if (candidate == element) { return(index); } else if (candidate < element) { var offset = index + 1; var position = SubChop2(element, array.Slice(offset, array.Count - offset)); return(position > -1 ? offset + position : -1); } else //if (candidate > element) { return(SubChop2(element, array.Slice(0, index))); } }
protected ScriptCommand(ArraySlice <byte> data, string name = null, int size = 0x04) { data = data.Slice(0, size); Offset = data.Offset; Size = data.Count; Type = TypeOf(data); Name = name; }
unsafe ArraySlice <float> inner(int val) { var mm = new UnmanagedMemoryBlock <float>(15, val); var addr = (IntPtr)mm.Address; var arr = new ArraySlice <float>(mm); return(arr.Slice(5, 5)); }
public void Fill() { var mem = new UnmanagedMemoryBlock <int>(1000); try { for (int i = 0; i < 1000; i++) { mem[i] = i; } var arr = new ArraySlice <int>(mem); var r = arr.Slice(50, 10); var rr = r.Slice(0, 5); r.ToArray().Should().ContainInOrder(Enumerable.Range(50, 10)); rr.ToArray().Should().ContainInOrder(Enumerable.Range(50, 5)); } finally { mem.Free(); } }
// https://github.com/DataDog/datadog-agent/blob/eac2327c5574da7f225f9ef0f89eaeb05ed10382/pkg/trace/traceutil/normalize.go#L98-L209 public static string NormalizeTag(string value) { if (value is null) { return(null); } if (string.IsNullOrEmpty(value) || Encoding.GetByteCount(value) == 0) { return(string.Empty); } char[] charArray = null; char[] upArray = null; char[] lowArray = null; int trim = 0; List <int[]> cuts = null; int chars = 0; int jump = 0; int i = 0; for (; i < value.Length; i++) { var c = value[i]; jump = 1; if ((c >= 'a' && c <= 'z') || c == ':') { chars++; goto end; } if (c >= 'A' && c <= 'Z') { if (charArray is null) { charArray = value.ToCharArray(); } charArray[i] = (char)(((int)value[i]) + ((int)'a' - (int)'A')); chars++; goto end; } if (char.IsUpper(c)) { if (upArray is null) { upArray = new char[1]; } if (lowArray is null) { lowArray = new char[1]; } upArray[0] = c; lowArray[0] = char.ToLowerInvariant(c); if (Encoding.GetByteCount(upArray) == Encoding.GetByteCount(lowArray)) { if (charArray is null) { charArray = value.ToCharArray(); } charArray[i] = lowArray[0]; c = lowArray[0]; } } if (char.IsLetter(c)) { chars++; } else if (chars == 0) { trim = i + jump; goto end; } else if (char.IsDigit(c) || c == '.' || c == '/' || c == '-') { chars++; } else { if (cuts is null) { cuts = new List <int[]>(); } var n = cuts.Count; if (n > 0 && cuts[n - 1][1] >= i) { cuts[n - 1][1] += jump; } else { cuts.Add(new int[] { i, i + jump }); } } end: if (i + jump >= 2 * MaxTagLength) { i++; break; } if (chars >= MaxTagLength) { i++; break; } } i--; if (cuts is null || cuts.Count == 0) { if (charArray is null) { return(value.Substring(trim, i + jump - trim)); } return(new string(charArray, trim, i + jump - trim)); } charArray ??= value.ToCharArray(); var segment = new ArraySlice <char>(charArray, trim, i + jump - trim); int delta = trim; foreach (var cut in cuts) { int start = cut[0] - delta; int end = cut[1] - delta; if (end >= segment.Count) { segment = segment.Slice(0, start); break; } segment[start] = '_'; if (end - start == 1) { continue; } segment.Slice(end).CopyTo(segment.Slice(start + 1)); segment = segment.Slice(0, segment.Count - (end - start) + 1); delta += cut[1] - cut[0] - 1; } return(new string(segment.Array, segment.Offset, segment.Count)); }
private static bool ProcessCodeBlock(ref ArraySlice <string> input, List <string> output, bool expectOutputBlock, int inputLine0) { var sink = MessageSink.Current; output.Add("~~~csharp"); int numInputLines = input.Slice(1).IndexWhere(s => s == "~~~"); if (numInputLines == -1) { sink.Write(Severity.Error, "Line " + inputLine0, "No matching end marker ~~~ for input code"); return(false); } var inputLines = input.Slice(1, numInputLines); output.AddRange(inputLines); output.Add(input[1 + numInputLines]); // ~~~ input = input.Slice(1 + numInputLines + 1); if (expectOutputBlock) { int line0 = inputLine0 + numInputLines + 2; // Ignore existing output block by skipping past it... // Just make sure the block exists and has a "Output of LeMP" comment if (input[0] != "" || input[1] != "~~~csharp") { sink.Write(Severity.Error, "Line " + line0, "Output code block is missing, so cannot replace it"); return(true); } if (input[2] != OutputComment) { sink.Write(Severity.Note, "Line " + (line0 + 1), "Leaving customized output block unchanged"); return(true); } int numOutputLines = input.Slice(2).IndexWhere(s => s == "~~~"); if (numOutputLines == -1) { sink.Write(Severity.Error, "Line " + (line0 + 1), "No matching end marker ~~~ on output code"); return(false); } input = input.Slice(numOutputLines + 3); } // Process input with LEMP! var outputCode = Process(MP, inputLines.Join("\n"), inputLine0); if (outputCode == null) { return(false); } if (outputCode.EndsWith("\n")) // remove extra newline { outputCode = outputCode.Substring(0, outputCode.Length - 1); } output.Add(""); output.Add("~~~csharp"); output.Add(OutputComment); output.Add(outputCode); output.Add("~~~"); return(true); }