Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,17 @@ namespace AutoRest.CSharp.Common.Output.Expressions.KnownValueExpressions
internal sealed record IntExpression(ValueExpression Untyped) : TypedValueExpression<int>(Untyped)
{
public static IntExpression MaxValue => new(StaticProperty(nameof(int.MaxValue)));

public IntExpression Add(IntExpression value) => Operator("+", value);
public IntExpression Minus(IntExpression value) => Operator("-", value);
public IntExpression Multiply(IntExpression value) => Operator("*", value);
public IntExpression DivideBy(IntExpression value) => Operator("/", value);

public static IntExpression operator +(IntExpression left, IntExpression right) => left.Add(right);
public static IntExpression operator -(IntExpression left, IntExpression right) => left.Minus(right);
public static IntExpression operator *(IntExpression left, IntExpression right) => left.Multiply(right);
public static IntExpression operator /(IntExpression left, IntExpression right) => left.DivideBy(right);

private IntExpression Operator(string op, IntExpression other) => new(new BinaryOperatorExpression(op, this, other));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace AutoRest.CSharp.Common.Output.Expressions.KnownValueExpressions
{
internal sealed record StringBuilderExpression(ValueExpression Untyped) : TypedValueExpression<StringBuilder>(Untyped)
{
public StringExpression Length => new(Property(nameof(StringBuilder.Length)));
public IntExpression Length => new(Property(nameof(StringBuilder.Length)));

public MethodBodyStatement Append(StringExpression value) => new InvokeInstanceMethodStatement(Untyped, nameof(StringBuilder.Append), value);

Expand All @@ -27,5 +27,7 @@ internal sealed record StringBuilderExpression(ValueExpression Untyped) : TypedV
public MethodBodyStatement Append(FormattableStringExpression value) => new InvokeInstanceMethodStatement(Untyped, nameof(StringBuilder.Append), value);

public MethodBodyStatement AppendLine(FormattableStringExpression value) => new InvokeInstanceMethodStatement(Untyped, nameof(StringBuilder.AppendLine), value);

public MethodBodyStatement Remove(ValueExpression startIndex, ValueExpression length) => new InvokeInstanceMethodStatement(Untyped, nameof(StringBuilder.Remove), startIndex, length);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright(c) Microsoft Corporation.All rights reserved.
// Licensed under the MIT License.

using System;
using AutoRest.CSharp.Generation.Writers;

namespace AutoRest.CSharp.Common.Output.Expressions.ValueExpressions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using AutoRest.CSharp.Common.Output.Expressions.ValueExpressions;
using AutoRest.CSharp.Common.Output.Models;
using AutoRest.CSharp.Generation.Types;
using AutoRest.CSharp.Output.Models.Serialization;
using AutoRest.CSharp.Output.Models.Shared;
using static AutoRest.CSharp.Common.Output.Models.Snippets;

Expand All @@ -32,14 +31,12 @@ private ClientUriBuilderProvider() : base(Configuration.HelperNamespace, null)
private readonly FieldDeclaration _uriBuilderField = new(FieldModifiers.Private, typeof(UriBuilder), "_uriBuilder");
private readonly FieldDeclaration _pathBuilderField = new(FieldModifiers.Private, typeof(StringBuilder), "_pathBuilder");
private readonly FieldDeclaration _queryBuilderField = new(FieldModifiers.Private, typeof(StringBuilder), "_queryBuilder");
private readonly FieldDeclaration _pathSeparatorField = new(FieldModifiers.Private | FieldModifiers.Const, typeof(char), "PathSeparator", Literal('/'), SerializationFormat.Default);

protected override IEnumerable<FieldDeclaration> BuildFields()
{
yield return _uriBuilderField;
yield return _pathBuilderField;
yield return _queryBuilderField;
yield return _pathSeparatorField;
}

private PropertyDeclaration? _uriBuilderProperty;
Expand Down Expand Up @@ -156,9 +153,9 @@ private IEnumerable<Method> BuildAppendPathMethods()
Assign(value, new InvokeStaticMethodExpression(typeof(Uri), nameof(Uri.EscapeDataString), new[]{ value }))
},
EmptyLine,
new IfStatement(Equal(new IndexerExpression(value, Literal(0)), _pathSeparatorField))
new IfStatement(And(And(GreaterThan(pathBuilder.Length, Int(0)), Equal(new IndexerExpression(pathBuilder, pathBuilder.Length - Int(1)), Literal('/'))), Equal(new IndexerExpression(value, Int(0)), Literal('/'))))
{
Assign(value, value.Substring(Literal(1)))
pathBuilder.Remove(pathBuilder.Length - Int(1), Int(1))
},
EmptyLine,
pathBuilder.Append(value),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ internal class ClientUriBuilder
private UriBuilder _uriBuilder;
private StringBuilder _pathBuilder;
private StringBuilder _queryBuilder;
private const char PathSeparator = '/';

public ClientUriBuilder()
{
Expand Down Expand Up @@ -42,9 +41,9 @@ public void AppendPath(string value, bool escape)
value = Uri.EscapeDataString(value);
}

if (value[0] == PathSeparator)
if (PathBuilder.Length > 0 && PathBuilder[PathBuilder.Length - 1] == '/' && value[0] == '/')
{
value = value.Substring(1);
PathBuilder.Remove(PathBuilder.Length - 1, 1);
}

PathBuilder.Append(value);
Expand Down
Loading